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):
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)
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.
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?
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