[UIPath] How to use Regex String Matches Activity to extract capture groups

The Matches activity is a helpful way to extract parts of a string using regex patterns. If you're new to capture groups, it's essentially a regex pattern that allows you to get values in between specified characters.


For instance, this pattern "\[(.*?)\]" allows you to get all values in between square brackets.

This means that for this string example,

Email Assignment: [2574] The UK votes to leave [EU]. What's next?

there are two matches (highlighted). These two values are stored in a collection of matches with type IENumberable<Match> To get the first match, you can use: Matches(0).Value
This will get you [2574]

But if you only need the value inside the brackets, you still need to clean-up the string in order to remove the brackets.

Now, a much simpler way to extract the first match without the brackets is by using the Match.Groups method.

Using the same example above, if I use: Matches(0).Groups(1).Value
It will return just 2574

Matches(0) will return the first match: [2574]
And Groups(1) will return the first group in the match: 2574
Groups(0) on the other hand would return the whole matched string: [2574]

So essentially, Matches(0).Groups(1).Value will return the first group in the first match.
If you want to get EU instead, you should use Matches(1).Groups(1).Value

You may experiment with regex using UiPath's built in Regex Builder (Inside Matches activity, click Configure Regular Expression) or you can go to regexstorm.net/tester online.
Share: