How can I use the Matches activity to execute a regex expression that will return the values found.
For example. The input string is something like "some text then (123) etc "
I want 3 values back: the part before the parenthesis, the value in the parens and the value after
I suppose you can always use the substring method for strings and get the 3 values you want. Just get the index of “(” and “)” and then you can use the substring method accordingly.
Finding out how to use Regex, (now that I’ve learned that Regex even existed while trying to figure out this issue) within UIPath opens up an entire world of powerful commands to be integrated.
That’s because Groups are a special property of the Match class; they only work if you use special “capturing groups” in your pattern. For example, suppose we want to test the input
abcdefghi123
with the pattern
def(ghi)(?<foo>\d{3})(mno)?
using the Matches activity. This will give you a list of Match objects (IEnumerable<Match>), let’s call it matches, with one element. Let’s call this first match match, so match = matches.First. You may also have to add System.Text.RegularExpressions to your imported namespaces for everything to work and autocomplete properly.
Now, the whole pattern matches the substring defghi123, which is stored in match.Value, but it also contains two unnamed groups and one named group “foo”. The unnamed groups are indexed from 1, so you will find that match.Groups(1).Value is ghi. The second of these, at the end of the pattern, is optional and was not present in the input; you can test for this with the property match.Groups(2).Success (False in this case). Named groups behave just the same but are accessed by string identifiers: match.Groups("foo").Value is 123.
For those wanting to know all the details (there are many, many more) about regular expressions in .NET, I suggest you start here.