Pattern Matching in APEX

Apex uses pattern matching and can be used for regular expressions – but the tests are from the Java Pattern class, see https://docs.oracle.com/javase/1.5.0/docs/api/index.html?java/util/regex/Pattern.html

The stages are :

  1. Build the regular expression and test it first
  2. Convert to a string
  3. Set up the code

Stage 1 – Build the regular expression and test it first

Let’s say you have an incoming file of strings of purchase orders that are always in the form of PO followed by 6 numbers but you also get extra text before and after. So the following are examples of incoming text:

  • PO123456 yyyy
  • abcde PO123456 fghi
  • abcedPO123456fghi
  • abcedPO123456/7890

https://regex101.com/ is a useful site to try out your patterns, but this is fairly easy example in that we can see that there might be some initial text (or not) followed by PO and then 6 numbers and then perhaps some additional text. The central PO followed by 6 digits is easy looking at the java document above we need PO/d/d/d/d/d/d

We might have some characters after and a generic character match is . and we can use * to denote either zero or more occurrences – so .* will be any set of characters. Similarly .* at the front will help remove any characters before the PO. So .*PO\d\d\d\d\d\d.* will find PO preceded or followed by other characters. Entering the regular expression into the https://regex101.com allows you to test various strings.

However there is an additional task because we want to pick out the PO number. We use brackets to highlight the information we actually want so .*(PO\d\d\d\d\d\d).* will pick (or group) out PO123456 from the rest of the information we do not need.

Stage 2 – Convert to a string

In apex we will need a string of .*(PO\d\d\d\d\d\d).* but we cannot enter / directly and this will need escaping with / so / becomes // – which can make reading the code a bit difficult – our string will be .*(PO\\d\\d\\d\\d\\d\\d).*

Stage 3 – Set up the code

The Apex code is fairly straight forward – create a pattern to use, use the Matcher class to try your string against the pattern and then look at the results. Pattern.compile is the pattern we are using, The Matcher class is used to load the text we need against the pattern. Importantly the matches() function needs to be executed to actually run the pattern against the text. This will return true or false showing if there is a match. The group(1) function of the Matcher class can then be used to show the first matching group. If you have 2 or more groups then use group(2) etc.

// Setup the pattern (regex)
Pattern pat = Pattern.compile('.*(PO\\d\\d\\d\\d\\d\\d).*');

// use the Matcher class to put the input string into the pattern matcher
Matcher pmatch = pat.matcher('abcedPO123456fghi');

// run the matcher by executing the matches() function which will return a true or false for matches
if (pmatch.matches()) {
  system.debug('Found reg ex match');
  system.debug(pmatch.group(1));
} else {
  system.debug('No match');
}

There are different ways to use patterns and for instance \d\d\d\d\d\d can be replaced with \d{6} which means a digit that occurs exactly six times.

Here are some other examples

  • ([a-zA-Z0-9_-]{5,12}) to test for a password that is composed of letters a-z (upper or lower case), numbers 0-9 or a – or a _ so my-PASS_WORD would pass this test
  • (\d+)hrs\s(\d+)mins will extract the hours and minutes in the form 2hrs 30mins or 12hrs 5mins

Useful links:

Leave a Reply

Your email address will not be published.