Regex101 vs UiPath Studio

Simple question i need clarification on:

Why can i write a regular expression in www.Regex101.com and get a 100% match on what i want to retrieve from the text but when i paste that expression to UiPath studio (Matches-activity) it either returns NULL or something else is wrong?

E.g. trying to get the name of the .txt file from a path and it gives me 100% match(Regex101):

But gives me NULL when i paste that exact expression to UiPath even tho the INPUT is correct (the file path string).

Thank you.

Hi.

I am not sure actually, but the Matches activity could work differently. I have seen what you are talking about a few times, where the pattern doesn’t return anything.

An alternative way to use Regex (and the method I use the most) is to use vb net syntax

System.Text.RegularExpressions.Regex.Match(text, pattern).Value
or System.Text.RegularExpressions.Regex.Matches(text, pattern) // to an enumerable type

If you have the Regex namespace imported, then you can just use Regex.Match() etc (without the full namespace), however it could pose issues on machines that don’t have it imported.

Anyway, you can use this method anywhere in your code throughout, like Assign activities or in If conditions.

So give that a try and see if you get better results. Additionally, if you do use the .Matches() you get an enumerable, so you need to run that through a For loop or output in a message box with String.Join(",",Regex.Matches(text, pattern).Cast(Of Match))
(assuming I got that right)

Regards.

1 Like

I haven’t used regex101, but different languages do have different regex formats. It’s possible that you’re checking the wrong “flavor” of regex at regex101. Make sure you’re using the VB.NET regex format in your expression

Good point Dave. Coincidentally enough, I had a regex solution I was working on myself and was doing some research on how to use modifiers. It doesn’t appear modifiers work here. So, for example using “/(text)/gm” will not work.

Essentially, I used the “?” next to “.*”, so like “.*?” and it solved my own problem which was trying to find all the matches between some words.

@Ibra

Regards

Thank you for your input guys.

But i really cant see what the VB.NET regex format is.

E.g. look at the attached picture above where i wrote an expression. That did not work in UiPath studio.
What is the essential ‘key’ that i have to change/add/remove before that syntax will work?

Will this return a string or the same output as if i used the Matches-activity?

System.Text.RegularExpressions.Regex.Match returns a ‘Match’ object. Adding ‘.Value’ as shown above will return a string.

As for testing, I usually use http://regexstorm.net/tester when testing my uipath regex, as that is a site specifically for .net

1 Like

If you want multiple matches then using the Regex.Matches should be the same as the Matches activity, which returns an enumerable of type Match. Add .Cast(Of Match) on the end of the enumerable to return each match.
For example, String.Join("|",matches.Cast(Of Match)) or For each m In matches.Cast(Of Match)

Also, like Dave said if you use .Match() to get the string use .Match().Value

Regards.

Thank you guys! It helped me understand it better now :slight_smile:

Have a nice day.