I have the following values for a custom picklist field

  1. Lower - L1
  2. Upper - L2
  3. Side - L3

What is the regex pattern to match the above values?

Mainly this pattern

   {anylength of string}{one space}{one hyphen}{one space}{L charecter}{one digit integer}

I have tried something this way but not working

String message = 'Upper - L2';
String regex = '[a-zA-Z\\sL\\s\\d{1}]*';
Pattern regexPattern = Pattern.compile(regex);
Matcher regexMatcher = regexPattern.matcher(message);
if(regexMatcher.matches() == true) {

2 Answers 2


You have tried to make the regex using a character class, but that's simply incorrect.

You should find something like the following is more appropriate:

^.+ - L[0-9]$

This is:

  • Matching the whole value (start at the beginning with "^" and end at the end with "$")
  • Allowing any sequence of at least one character with ".+"
  • explicitly matching space, hyphen, space (this doesn't match tabs etc.)
  • explicitly matching "L" (this doesn't match "l")
  • allowing a single numeric digit between 0 and 9
  • The ^ and $ are not strictly necessary for matches, as that method sets the match boundary to the entire string (but not harmful in this case, either).
    – sfdcfox
    Commented May 27, 2020 at 8:00

A single character class isn't going to help you here, as that will simply match anything, and you specified "*", meaning zero or more, so it'll even match an empty string!

You need the specific pattern to match, so:

String regex = '[a-zA-Z]+ - [a-zA-Z][0-9]';

Which covers all of your examples. The spaces and hyphen can be written normally, the first part specifies only letters (at least 1 because of "+"), and exactly one letter and number at the end.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .