Using regular expressions in the development of UiPath
This article describes how to use regular expression when developing processes.
Regular expression (regex) is a pattern (sequence of characters and metasymbols) which corresponds (or not) to the sequence of characters in the text. As a rule, regular expressions are used to specify the rule of searching for a sequence in the text.
The service was used to check the regular visions https://regex101.com/
Places of use
Regular expressions can be used:
⢠For the comparison of the corresponding values;
⢠In selectors;
⢠For changing the values;
⢠Data retrieval;
These are the main ways of using regular expressions in UiPath.
Letâs proceed to the applications.
Regular expressions to compare the corresponding values
Create a text file that contains the row.
âHello, my name is Bohdan Ovcharenko, Iâm 20 years oldâ.
Check if the developer has entered his age by means of regular expressions.
Create a process using the âis matchâ activity and the âRegex.IsMatchâ method.
Letâs create the changes:
The process code looks like this:
Setting up the activity âRead Text Fileâ.
Setting up the activity âIs Matchâ.
We also carry out the operation of assignment through activity âAssignâ.
The result is outputted through Boolean changes. Check the correctness of the search by running the process.
As we can see - we found the necessary data for us.
Letâs conduct another test to check the presence of three digits with the help of the activity âIs Matchâ.
The result is unsuccessful, because there is no such text.
Regular expressions in selectors
More theoretical information can be found here About RegEx Search
In our case, we will write a process that will click on the LinkedIn link in Edge browser, but the name of the link can be 2 variants:
The selector will also be different.
In this case it is necessary to prescribe possible changes by means of the structure :
matching:name=âregexâ
In this way we write down the letters that change.
Regular expressions for changing the values
Symbol substitution is done using the method or the ReplacĐľ activity.
Letâs take the application from 1 point and replace âIsMatchâ with âReplaceâ.
The arrangement of the âReplaceâ asset looks like this:
We replace the text after the first comma with âI am RPA Developerâ.
The output change must be STRING, in our case, we just overwrite the existing text.
As in the first point , you can use the operation of assignment, it looks like this:
Replace âHelloâ with âHi.â
The code looks like this:
After starting the process, we got the same result:
Regular expressions for data retrieval
You can extract individual values by using the âMatchâ method or the âMatchesâ asset.
Letâs take the application from 1 point and replace âIsMatchâ with âMatchesâ.
Create a text file with the line âHello, my name is Bohdan Ovcharenko, I QA Engineerâ.
The activity of Matches have the following appearance:
Since the output of this activity is a variable list, we use For each to output the data.
As in the first point, you can use the operation of assignment, it looks like this:
The code looks like this:
With the help of the âMatchesâ activity and the regular expression âQA.*â we âcutâ everything from the text âQAâ and after it.
And using the âRegex.Matchâ method and the regular phrase â.*(?=QA)â we âcutâ everything that is before the text âQAâ.
We got the same result:
Thatâs all!
Successful developments!