How to use matches to extract values using regex

uiautomation
studio

#1

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


Using wildcards in an 'Click text' activity
#2

This regex should work:

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

Live:

https://regex101.com/r/q3tElu/1


#3

Yes … but How do I use that in uipath?


#4

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.


#5

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 )


#6

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


#7

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


#8

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)


Regex to string, HOW?
URL from mail body
Get only six digits from string
#9

So cool! Thank you thank you thank you!


#10

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


#11

Hey @alice

Please try this and let me know :slight_smile:

regMatces.Cast(Of Object).First.ToString

Regards…!!
Aksh


#12

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


#13

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

The output variable is |Enumerable type,


#14

Regex should go between quotes in pattern property :slight_smile:

Regards…!!
Aksh


#15

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!


#16

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.


#17

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


#18

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


#19

Still looking for an answer on this?


#20

Yes, please.