How to use matches to extract values using regex

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

3 Likes

This regex should work:

[1]+|(?<=().+?(?=))|[^)]+$

Live:


  1. ^( ↩︎

3 Likes

Yes … but How do I use that in uipath?

You asked for Matches activity in your post, use it.

Enter your string and regex pattern.

Ctrl+k in the output and enter the variable name(ienumerable)

Do for each ienumerable, type argument as object.

3 Likes

It works almost perfectly … for input string “abcdef (49)”
I get item(0)= abcdef
item(1) = " "
item(2) = 49)

I thought I understand regex’s but I cant figure out this pattern
Do you understand why it’s matching the )

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.

Regards,
Sachin

1 Like

For me it is only giving first past but substring working for me. Incase if you wanna try :
String Match.xaml (8.3 KB)

Regards…!!
Aksh

1 Like

Probably the regex got changed(tried to bold it and changed back) when I pasted, but the one in the link is correct

regex_Matches.xaml (6.2 KB)


  1. ^( ↩︎

4 Likes

So cool! Thank you thank you thank you!

1 Like

I have use “Matches” activity, and I want to catch the first element of the result.
Did you know how can I do?
Thank you

1 Like

Hey @alice

Please try this and let me know :slight_smile:

regMatces.Cast(Of Object).First.ToString

Regards…!!
Aksh

10 Likes

Thank @aksh1yadav! It works.Did you know why we could not use Cast.Groups(0)?

2 Likes

I’m trying to put regex into a matches box and I’m getting this error,

The output variable is |Enumerable type,

Regex should go between quotes in pattern property :slight_smile:

Regards…!!
Aksh

3 Likes

Thanks so much!! That worked.

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.

I really appreciate your help! Thank you!

3 Likes

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.

7 Likes

Thank a lot, @sfranzen, now it’s clear to me.

1 Like

Hi, can I know how do one determine the pattern? What is with the Group(1)?

3 Likes

Still looking for an answer on this?

1 Like

Yes, please.