Using the build in activities ‘Matches’ and ‘Is Match’ is often useful, however there is a much more simple solution (at least for a lot of cases) to extract data out of text with RegEx in UiPath.
Consider the following text: “Amout due $2,000.00, please pay before 04/24/2020”, where the format always will be the same, but the amount will change. We want to extract the amount and store it in a string, i.e. “2,000.00”.
First assign the two variables in the variables manager:
- ‘strInput’, a string where we store our input
- ‘strOutut, a string to hold the value of the extracted amount
Use RegEx to assign a value to our output string, ‘strOutput’
Drag in an ‘Assign’ activity (Fig. 2) and set the left side to ‘strInput’ and the right side to ‘System.Text.RegularExpressions.Regex.Match(strInput, “(?<=\$)(\d+,?)+(\.\d+)”).Value’ (Fig. 3).
Lets dig into the expression on the right side:
- System.Text.RegularExpressions is the Microsoft .NET namespace
- Regex.Match is the method (covered here https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=netframework-4.8)
- strInput is the string to search in
- “(?<=\$)(\d+,?)+(.\d+)” is the RegEx pattern, we use to extract the amount
- (?<=\$) is a postive lookbehind, where we specify, we want to look behind a ‘$’
- (\d+,?) then we look for one or more digits (\d+) and one or none thousand delimiter
- + one or more of the preceeding patters (otherwise it would stop after ‘2,’
- (\.\d+) a decimal delimiter (\.) and one or more digits (\d+)
Write the output out
Now just drag in a ‘Write Line’ and set the value to our Output Variable, ‘strOutput’
And our result is…
And then we’re done (Fig. 5). The workflow will also work with all other values.
Additional Regex material
I’ve created this video tutorial about RegEx, if you want further informations: