Regex expression as variable

Hello all,

I have a question about Regex and UiPath.
My plan is to have a config file, where I can store all the regex patterns needed to extract specific data from pdf files.
My Excel:


Now, in UiPath studio, I want to use the Assign activities, to run that regex formula/expression
image

My idea is, that since myExcelvariable will have the regex expression from Excel file, the SUC value will be the result of the regex expression. Which will be a string.
I am thinking that UiPath will run the regex expression like :
image
Is this possible?
If no, any ideas on how to do this?

Environment: Community Studio, 2020.4.1

Thanks a lot!

@wasea
Short answer:
RegEx Pattern only as String: YES
Programming Statement /VB.Net Code Lines: NO

in general it is doable:

  • define in Excel 2 columns:
    Key, Pattern
    A, ABC{1}

ensure that the excel is readin into a datatable and that you have access to the different patterns

then RegEx.Match(“YourString”, YourPatternVar)… can use it

Let us jknow if you need more help on this

1 Like

Thanks @ppr,
My pattern is actually: “/\s\sNo:\s\s(\d+)”

In Assign, my regex is like below:
Regex.Match(InvoiceTXT,NUmarFacRegex).Groups(1).Value.ToString

UiPath should see it like this:
Regex.Match(InvoiceTXT,“/\s\sNo:\s\s(\d+)”).Groups(1).Value.ToString

Tested already, if I use the pattern directly, it works OK. When I put the variable, that contain the pattern, not working.

Thanks.

@wasea
Have alook here
grafik

it should prototype your case and the pattern is used as a variable

Well, if I define in studio a variable, that contain a pattern, and in my regex I add that variable, it is working.
Still, I need to troubleshoot, why is not working with the value from my excel file.
Using Write line, I can see that the value from my excel field, is correct. Still, when I add the variable based on my entry from my excel file, I do not get anything.

More troubleshooting…

@wasea
often some blanks are added by the value retrieved from Excel, so use a proper toString and a trim
check in debugger /watch panel the excel row content slashed, backslashes and the plus sign is hanndled correctly

My fix seems to be double quotes for the value imported from Excel file.
“”+myvariable.ToString+“”

Anyway, this is solving half of my scenario, only the pattern.

I was thinking, if in the assign activity I can add the entire Regex expression from Excel like:
System.Text.RegularExpressions.Regex.Match(InvoiceTXT,“SUC\s+(\w+)”).Groups(1).Value.ToString

If this is not possible, well, I will stick to the pattern only.

Thanks a lot @ppr.

Vasile.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.