2

Salesforce – Apex – Working With Regular Expressions

 1 year ago
source link: https://cwestblog.com/2022/12/23/salesforce-apex-working-with-regular-expressions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Regular expressions are a great way to parse, match and modify strings of all sorts. Let’s learn some different ways that we can use them in Apex by way of some examples that you can run in the Developer Console.

Pattern.matches(…)

First we will learn how to use Pattern.matches(…) for pattern matching. Let’s say that you want to test a string to see if it looks like a number. Try running the following as anonymous code in the Salesforce Developer Console:

Running the above code will output the following:

As you can see we are able to see if the given string matches the regular expression but it isn’t the most efficient because every time the function is called we are compiling the regular expression on the fly. Let’s see how we can get around that in the next section.

Pattern.compile(…)

Now we are going to be using Pattern.compile(…) along with Pattern#.matcher(…) and Matcher#.matches() for more efficient pattern matching. Let’s say that we want to do the same thing that we did above but this time we dont want to have to compile the regular expression on the fly every time. In that case we can do something like this:

As you can see in this example, this function is great to test a regular expression against an entire string. On the other hand, this will not work if you want to simply test part of a string against the regular expression.

Matcher#.find()

By using an instance of a Matcher that is bound to a regular expression Pattern and a String you can determine whether or not the regular expression can be found in any part of the given string. Here is an example:

If you run the above code in the Salesforce Developer Console as anonymous Apex the following will be output:

You may have noticed that the regular expression starts with (?i). This is a flag indicating that the regular expression is going to be case-insensitive.

What else can we do Matcher?

Matcher#.group(), Matcher#.start() and Matcher#.end()

In reality, by using the Matcher#.find() function we are looking to see if a first match can be found but we do not have to stop there. Let’s say that we want to find all of the instances of a vowel. Building on our previous example we can go further and use 3 additional function to get specifics:

Running the above will result in the following output:

As you can see from the results, the Match#.group(…) function can be used to either get the entire group that was found or a capture group (if you specify the index of the capture group). The Match#.start(…) function can be used to either get the starting index of the entire group that was found or the starting index of a capture group (if you specify the index of the capture group). The Match#.end(…) function can be used to either get the ending index of the entire group that was found or the ending index of a capture group (if you specify the index of the capture group). It is important to note that although you can specify a named capture group, you can only reference it by name when using replaceAll() or replaceFirst().

Using Named Capture Groups

Let’s say that we want to consistently separate first names from last names in a string that contains both and we want to use named capture groups to do it:

Of course, the above is a simple example and shouldn’t necessarily be used in production code but it shows us how to reference named capture groups. The output is as follows:

As you can see we are able to reference named capture groups by using ${name}, where “name” is replaced with the actual name of the capture group. You could also reference the same capture group by the number of the group. For example, in the Apex code we could have used this instead:

That could would still produce the same output as before, but, of course, it doesn’t make much sense to reference a named capture group by index unless you are just trying to save on the length of your code.

You can make backreferences to named capture groups by using \k<name>, where “name” is replaced with the actual name of the capture group. Here is an example showing how to use backreferences:

The above results in this being printed out:

There are many other things that you can do with regular expressions in Apex. Hopefully this gives you a good head start. Happy coding! 😎


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK