Regex output in Matches box

Hello,

I’m trying to understand the RegexBox “(.*?) Matches”

In input i sent a String Variable
My pattern is working fine as regex (finding special caracter to return the X caracters before)
The result is a brand new Variable

When i’m trying to confirm that my expression is working fine it returns “System.Linq.Enumerable+d__94`1[System.Text.RegularExpressions.Match]” as a result. This is a quite a big message compared to the few letters that i was expecting :slight_smile:

Any idea ? I tried to modifiy my variable type, but UI Path is requesting “IEnumerable” type

Thank you

12 Likes

Hum, probably not so easy ?

Should i have to consider it as issue ?

In the meantime i tried to modify the “RegexOption” filed, but same problem.

2 Likes

Ok ! Found it …

So, here is the trick :

The output is a “Collection” (meaning a data table ?). So to work with your “collection” (in my case 1 word) you have to “Assign” it to a String Variable like this :

yourVariable=collectionName(0).ToString

“(0)” to call the first result

16 Likes

You are a star CG00. I have been trying to figure this out for hours and now, thanks to you, it works.Thank you!!

what if I want to print all the result from that collection?. How should I do that?

1 Like

You can use For each activity. In Values option you put the Collection of Matches. And change the variable type of item to System.Text.RegularExpressions.Match. And then a Write line activity.
Screenshot_86

6 Likes

It works. Thanks a lot

Hi, I try to write all the items but 2 with conditions into a txt file.
I replace the “Write line” with Write Text" but it doesn’t won’t work because it is replacing the file every time…

How to do that? Thanks

You can use the ‘Append Line’ activity instead of the ‘Write Text’ activity.

Just specify the name of the file you’d like to use (it will create a new one if it doesn’t exist already) and it should work. If you want each match to be on a new line, you can change it to item.tostring + environment.newline.

2 Likes

Dave, it works perfectly :smiley: !! Also, regex is a better and more straightforward approach to my case. Learning a bit more everyday - Thank you very much! You the best :sunny:

Hi Dave, turns out I will have to put the number in a format like this:

123;456;756;

but by using append line, even if I use item.ToString + “;”
it shows:

123;
456;
756;

Is there another activity that I can use to achieve this within the matches forloop? Thanks!

use append text instead of append line

EDIT: It is supposed to be for word documents, but I think it works for .txt files as well. I’ve never used it though.

Another approach would be to store it as a string or list of strings, then write the full string out in a write text file activity after the For Each loop completes

Thanks for your respond! My long time concern is how to append the variable in the list outside of a forloop.
For example in python, we can do

y =
for x in xs:
y.append(x)

what about uipath? After some research, I could do

y.add(item.ToString)

but then what activity should I wrap this to…???

so I declared y as a string, then how can I append it in the forloop?

Thanks!

Hi Folks,

i’m trying to use matches to find words in a txt but i need to use wildcards but i have no idea.

I.E. in a txt i have those words:

tei.pdf
tei_1.pdf…
\tei__.pdf

the target is to get the words :

tei.pdf
tei_1.pdf
tei__.pdf

so i need to enter wildcards between “tei” and “pdf”

this is the flow…

please, could you help me??

thanks in advance!!

I believe you can use the following: tei.*\.pdf

Assumptions: There won’t be any newline characters between the first t in tei and the last f in .pdf

How it works:

  1. tei - This finds the 3 characters tei that must be in a row without any other characters between them
  2. .* - This uses the . character which is a wildcard that matches everything except newline characters in conjunction with * which matches the preceding character 0 or more times
  3. \.pdf - This finds the 4 characters .pdf that must be in a row without other characters between them. Since . is a special character in regex, it must be escaped by preceding it with a backslash \

Regex doesn’t use the asterisk * as a wildcard for any character like you’re used to, an approximation of the * in VB.NET regex would be the period/full stop character . which matches any character except newline.

I’d recommend checking out https://www.regular-expressions.info/ for a quick tutorial on how to use regex. I usually test everything out at .NET Regex Tester - Regex Storm (which is where I tested my answer to your question).

I used the following string to test if I could pull out your 3 examples and it did it without a problem.

asdfuhiaushxjioahzxjifhqweiohf jaskdhfjkashodfuihwauiefhcjfj
asdrfhuihgyigybnkatei.pdfchgaoyerjhbzxnf
uxuuyebjhgvu3e8h989ycc7v7b78b89
asduhfiiuhjckjcjkbmcm,sajkhasdfuie
tei_1.pdf
asdjkfcfhibjklahbccbhasdiosfdajdfiopuasdfiuopashdfuiasipdghiasdghiuoh11377577c986vasdghf8907ag90a78yrf123n.s.zdkjfhsioetei__.pdf
2 Likes

Sorry for the delay Dave, your solution is much pretty, and thanks for the links :wink: :wink:

BR.