THE PURPOSE OF THIS POST IS TO HELP NEW REGEX USERS
If you need help with Regex be sure to provide in your post, a Sample, the Output and the Pattern (as much as you know).
CONTENTS:
Section : I need a Regex Pattern ASAP! - My first Regex Post
Section : Regex samples that you can use!
Section : I need help applying regex in UiPath
Section : Regex Matches and Groups
Section : Learn Regex in 60 mins
Section : HELP Regex isn’t working in UiPath!!! – Regex Troubleshooting
CLICK HERE to Download this UiPath Studio REGEX DEMO (13.0 KB)
Or
Watch this UiPath Regex Tutorial in less than 2 mins:
Regex + 1 Assign activity = Result (in String)
SECTION : I need a Regex Pattern ASAP! - My first Regex Post
If you truly need help please provide the following AS A MINIMUM.
- SAMPLE - Provide a sample (or 3) of your raw text in your post (or upload a notepad file or PDF )
- OUTPUT - Provide the output after the raw text (bolding of the output helps also – optional )
- PATTERN - What is always consistent? Tell us as much as possible about the pattern of the text (example: It will ALWAYS be 2 capital letters, a space, then 4-6 numbers)
Bonus:
- Provide as much information as possible. Every little bit helps and you will get a stronger Regex Pattern in less posts.
- Will it change? (Capital letters, combination/amount of words/numbers, how many spaces does it have, is it consistent?)
- Is there multiple matches or just one in your text?
- Are you using OCR?
- Can you use Regex in UiPath?
Regex relies heavily on finding a pattern that is constant and specific enough to match all occurrences but without false positives.
Spend 5-10 minutes making a quality post and I GUARANTEE it will save you time in replies and you will have an answer MUCH faster from a UiPath Forums Hero - they might even give you back an actual working UiPath solution (it’s a great community full of Champions so save everyone some time and submit a quality post)
Here is an example forum post:
" Hi All
I need a regex pattern to grab the state and postcodes from the below text.
- Sample text provided below
- The expected output I am seeking is below:
- RI 55924
- VA 34075
- AK 86847
- The pattern of the text: The State will always be two capital letters, a single space and the ZIP code will always be 5 numbers following that.* (AB 12345)
Any help would be appreciated.
Thank you.
Sample text:
Dave Martin
615-555-7164
173 Main St., Springfield RI 55924
davemartin@bogusemail.com
Charles Harris
800-555-5669
969 High St., Atlantis VA 34075
charlesharris@bogusemail.com
Eric Williams
560-555-5153
806 1st St., Faketown AK 86847
laurawilliams@bogusemail.com"
End of post…
You will have a response from the UiPath community with an answer in no time! [A-Z]{2}\s[\d]{5}
Remember, Sample, Output and Pattern. SOP.
Check out this great post about asking the right question in the UiPath Community Forum
SECTION : Regex samples that you can use!
(I STRONGLY recommend watching this video first - Learning and Understanding Regex)
Click the hyperlinks below for the full sample text.
*This is not an exhaustive Regex list. If you are unsure if you have a robust pattern, make a post to verify.
Anchors
When to use: When you have a fixed word that wont change and need to match the string after or before.
Anchor Samples below (will grab the word John Smith):
Words in the brackets can be replaced but MUST be exact (or literal).
Name Example 1:
Sample:
Payee: John Smith
Regex pattern: (?<=Payee: ).*
Comments: Fine to use if it’s the only string on the current line.
Name Example 2:
Sample:
John Smith – Payee
Regex pattern: .*(?= - Payee)
Comments: Fine to use if it’s the only string on the current line.
Name Example 3:
Sample:
Payee - John Smith - Account
Regex pattern: (?<=Payee\s-\s).*(?=\s-\sAccount)
Comments: This will capture everything between the words “Payee – " and " – Account”. This solution should be fine to use as long the words Payee and Account are constant.
Account Numbers Example
Samples:
145-567-981
145 567 981
145.567.9815
Regex pattern: \d{3}.\d{3}.\d{3,4}
Comments: The above pattern will match 3 digits followed by anything followed by 3 digits followed by anything followed by 3 or 4 digits.
Date Examples
Samples:
19.01.2020
19-01-2020
19,1,2020
Regex Pattern: \d{1,2}.\d{1,2}.20\d{2}
Comments: This pattern will work for the next 100 years and will match all three examples above (because of the “.”)
Time Examples
Samples:
08:01
8:22
8:03;23
Regex Pattern: \d{1,2}:\d{1,2}
Comments: 1 or 2 digits separated by a “:” followed by 1 or 2 digits.
Email Examples
Email Regex can be very challenging and can change depending on domains.
Below is a sample solution but test, test, test to ensure its fit for purpose.
Samples:
JohnSmith@gmail.com.au
John.Smith@hotmail.edu.com
John.Z.Smith@Regex.is.cool.com
Regex pattern: ([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+)\b
Comments: Ensure you have tested that it will work with your email domains and so on.
SECTION : I need help applying regex in UiPath
Watch this 10 min video from @AndersJensen (Best YouTube Channel for UiPath in my opinion)
This is the process:
- You will need to first get your raw text into UiPath.
- Then you will need to use a ”Matches" activity.
- The Pattern will be the Regex Pattern/solution from the forum post.
- Input will be your Raw Text variable
- Result will be the output which is a variable type called an “IEnumerable” .
“What is an IEnumerable?” you say, well:
- An IEnumarable is a collection of results (like an array).
- Even if you only have one result it will still be treated as a collection.
- To get the result, you will need to type a digit in brackets after to reference the result and group. 0 = is the first result only, 1 = the second result:
- VARIABLE(0).Tostring
- VARIABLE(0).Value
Download this UiPath Regex Demo I made so you can see what to do.xaml (13.0 KB)
.
…
…
SECTION : Regex Matches and Groups
When using Regex you will always have a full match but sometimes your Regex pattern might be made up of 1 or more groups. The number of groups can be 0. Having groups may also be an intentional design (to make the pattern robust) so you can grab elements later – like the year or month separately.
Take a look at this solution for an example
To get Group 1:
INSERTVARIABLE(0).Groups(1).ToString
To get Match 2:
INSERTVARIABLE(0).Groups(2).ToString
To get Match 3:
INSERTVARIABLE(0).Groups(3).ToString
Then use an assign activity and update the capital letters with the variable from the Result field in the Matches Activity.
Download this UiPath Regex Demo I made so you can see what to do.xaml (13.0 KB)
.
…
…
SECTION : Learn Regex in 60 mins (and understand Regex for next time )
To learn Regex there are a number of tutorials on YouTube but I STRONGLY recommend these videos:
To Understand and Learn Regex watch this video - 38mins:
Regular Expressions (Regex) Tutorial: How to Match Any Pattern of Text. I personally watched this video and started here. Tip : take screenshots and save them in MS Word
How to apply those learnings in UiPath watch this video - 11mins:
UiPath | Matches and Regex | Simple and Complete Tutorial. This tutorial is simple and concise. One of MANY amazing tutorials by the great @AndersJensen!
To Practice Regex, use the samples from the 1st video’s description and head over to the following sites:
Please note - the MOST compatible site with UiPath is .NET Regex Tester - Regex Storm NOT www.Regex101.com. There are differences between Regex101.com and UiPath.
Regex101.com is more popular because of the following features:
- share\save your patterns with users on the forums. Using the “Save Regex” function (ctrl + s) in the top left of the window – like this custom save.
- understand your regex pattern/result from the Explanation section (Red circled section to understand).
- improve your pattern using the Quick Reference section (Green circled section) – its a great tool and I still use it regularly to refresh/brush-up.
SECTION : HELP Regex isn’t working in UiPath!!! – Regex Troubleshooting
On occasion reading a PDF (or other documents) and storing it as a string will cause an error with UiPath.
UiPath will give you error messages like:
- “Object reference not set to an instance of an object”, Or
- “Illegal characters found in path”.’
1st step is to always triple check your Properties (Regex input, Regex Pattern and the Output).
Some Possible cause/s:
- Possible Cause 1: One possibility is you didn’t get a match and you need to check your sample data. Is the text mandatory? Is your Regex Pattern robust? Are you using OCR?
The solution: You need to check the count of the results to a message box. To do this insert the following into a message box:
OUTPUTVARIABLE.Count.ToString
(Replace capitals with your actual Result/Output variable in the Matches Activity). Also use an IF condition to error handle your workflow (see image).
Like user:ClaytonM has suggested here in this post.
- Possible Cause 2: Another reason is on occasion UiPath thinks there are invisible characters (whether there is or not) and can’t find a match or process because of the illegal/special characters. This can happen with a folder path also. (Please don’t ask why because I don’t know )
The Solution: – Is simple, you must trick UiPath by “CLEANING” your variable using the replace method (Use a Replace Activity to filter/remove unwanted characters).
Check out this UiPath Regex Demo with a CLEAN STRING example.xaml (13.0 KB)
BEFORE:
strDirtyVariable= Dave Martin
615-555-7164
173 Main St., Springfield RI 55924
davemartin@bogusemail.com
strCleanVariable = System.Text.RegularExpressions.Regex.Replace(strDirtyVariable, “[^a-z A-Z 0-9]”, “”)
This will replace all characters except these: (^a-z A-Z 0-9) with nothing (“”). Update the brackets in the above as necessary.
AFTER:
strCleanVariable = Dave Martin6155557164173 Main St Springfield RI 55924davemartinbogusemailcom
This text will now be 100% suitable for Regex. If you wanted to keep the spaces just add a “\s” to the “[ ]” brackets.
SECRET BONUS SECTION: Celebrate you Regex Champion!
Thanks for reading this far
Please bookmark this post, show it some support by clicking the and share it with new users.
Other MEGAPOSTs worth looking at:
Please take a look at these other valuable fantastic MegaPosts:
- Regex Cheatsheet by (@)ppr for more “System.Text.RegularExpressions” Regex methods and options
- String Manipulation by (@)Adrian_Star for everything on String manipulation.
- DateTime MegaPost by (@)Palaniyappan. This is very handy post for all things Date Time related.
- All About DataTable. This is another fantastic post by (@)Palaniyappan for a variety of datatable uses cases.
Feel free to message me if you have a Regex problem but make sure you provide SOP as per Section (Sample, Output, Pattern).
Also, Rookie UiPath Developers you REALLY need to subscribe to @AndersJensen on YouTube (here is a link to his channel) and check out his dedicated playlist for Rookies (UiPath Tutorial: The Basics for beginners). Anders has the best UiPath content on YouTube in my opinion (that’s a whole other post though).
Remember there are PLENTY of Regex legends on the forums! (I am simply a Regex Lover and aspiring to help as many people as I can)
The forum champions I see are: supermanPunch @Palaniyappan, Pratik_Wavhal, @ppr, msan, @Yoichi, bcorrea, Pradeep_Shiv, Pablito, lakshman, Arpit_Kesharwani, Anthony_Humphries and mzahid).
I can only tag 4 users in this post but just put a ‘@’ in front of the usernames… All are amazing in my book
Hopefully you found this post helpful and the standard of Regex posts improves in the forums
Happy Automation!
Cheers
Steve