Regex help tutorial MEGAPOST – Making your first Regex post, Reusable Regex Patterns, Regex Troubleshooting, Sample Workflow and more

The purpose of this post is to help New Regex users :blush:

I see a lot of Regex posts seeking help but very little effort is made in their post. Give us, Sample, Output and Pattern.

CONTENTS:
Section :one: : I need a Regex Pattern ASAP! :fast_forward: - My first Regex Post :boy:
Section :two: : Regex samples that you can use! :nerd_face:
Section :three: : I need help applying regex in UiPath :raised_hand:
Section :four: : Regex Matches and Groups :people_holding_hands:
Section :five: : Learn Regex :green_book:
Section :six: : Regex isn’t working in UiPath :scream: – Regex Troubleshooting

Download this UiPath Regex Demo I made so you can see what to do.xaml (13.0 KB)

SECTION :one: : I need a Regex Pattern ASAP! :fast_forward: - My first Regex Post :boy:

If you truly need help please provide the following AS A MINIMUM.

  1. SAMPLE - Provide a sample (or 3) of your raw text in your post (or upload a notepad file or PDF )
  2. OUTPUT - Provide the output after the raw text (bolding of the output helps also – optional )
  3. PATTERN - 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?
  • 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 :crossed_fingers: it will save you time in replies and you will have an answer MUCH faster from a UiPath Forums Guru - they might even give you back an actual working UiPath solution :open_mouth: (it’s a great community :white_check_mark: full of gurus :nerd_face: so save everyone some time :clock1030: 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
    • AK86847
  • 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 123456)

Any help would be appreciated. :slight_smile:

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.

SECTION :two: : Regex samples that you can use! :nerd_face:
(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: ).*
Regex101.com link
Comments: Fine to use if it’s the only string on the current line.

Name Example 2:
Sample:
John Smith – Payee

Regex pattern: .*(?= - Payee)
Regex101.com link
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)
Regex101.com link
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
Samples:
145-567-981
145 567 981
145.567.9815

Regex pattern: \d{3}.\d{3}.\d{3,4}
Regex101.com link
Comments: The above pattern will match 3 digits followed by anything followed by 3 digits followed by anything followed by 3 or 4 digits.

Dates
Samples:
19.01.2020
19-01-2020
19,1,2020

Regex Pattern: \d{1,2}.\d{1,2}.20\d{2}
Regex101.com link
Comments: This pattern will work for the next 100 years and will match all three examples above (because of the “.”)

Time
Samples:
08:01
8:22
8:03;23

Regex Pattern: \d{1,2}:\d{1,2}
Regex101.com link
Comments: 1 or 2 digits separated by a “:” followed by 1 or 2 digits.

Email
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
Regex101.com link
Comments: Ensure you have tested that it will work with your email domains and so on.

SECTION :three: : I need help applying regex in UiPath :raised_hand:

Watch this 10 min video from @AndersJensen (Best YouTube Channel for UiPath in my opinion)

  1. You will need to first get your raw text into UiPath.
  2. Then you will need to use a ”Matches" activity.
  3. The Pattern will be the Regex Pattern/solution from the forum post.
  4. Input will be your Raw Text variable
  5. Result will be the output which is a variable type called an “IEnumerable” .
  • An IEnumarable is a number of results.
  • Even if you only have one result it will still be treated as a collection.
  • To get the result, you will need to type, (0 is the first result only, 1 is the second result):
    • VARIABLE(0).Tostring
    • VARIABLE(0).Value

image

Download this UiPath Regex Demo I made so you can see what to do.xaml (13.0 KB)
.



SECTION :four: : Regex Matches and Groups :people_holding_hands:
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 intentional (or its required to make the pattern more robust) so you can grab elements later – like the year or month separately.

Take a look at this solution for an example
image

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.

See the Attached workflow for a demo
UiPath Regex Demo.xaml (13.0 KB)
.



SECTION :five: : Learn Regex :green_book: (and understand Regex for next time :stuck_out_tongue_winking_eye: )

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

How to apply those learnings in UiPath watch this video - 11mins:
UiPath | Matches and Regex | Simple and Complete Tutorial by the great @AndersJensen

To Practice Regex, use the samples from the 1st video’s description and head over to:
http://regexstorm.net/tester - Please note this is the MOST compatible site for Regex Patterns in UiPath.
www.Regex101.com - Please note this site is mostly compatible with UiPath but is fantastic to learn from using the below sections:
The Explanation section (Red circled section to understand) to understand your regex pattern/result.
The Quick Reference section (Green circled section) to improve your pattern – its a great tool and I still use it regularly.

You you can also make and share Regex patterns to the forums using the “Save Regex” function (ctrl + s) in the top left– like this.
.



SECTION :six: : Regex isn’t working in UiPath :scream: – 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”.’

image

image

1st step is to Triple check your Regex input, Regex Pattern and the Output.

Some Possible cause/s:

  • Possible Cause 1: One possibility is you didnt get a match and you need to check your sample data. Is the text mandatory? Is your Regex Pattern robust?

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 ClaytonM has sugguested here in this post.

  • Possible Cause 2: Another reason is on occassion UiPath thinks there are invisible characters (whether there is or not) and can’t find a match or process because of the illegal/special character. This can happen with a folder path also. I don’t why this happens (please don’t ask :laughing:) …

The Solution: – Is simple, you must CLEAN your variable using the replace method.

Check out this UiPath Regex Demo with a CLEAN STRING example.xaml (13.0 KB)

Essentially you need to use a ‘Replace’ activity to remove the unwanted characters.

BEFORE:
DirtyVariable= Dave Martin
615-555-7164
173 Main St., Springfield RI 55924
davemartin@bogusemail.com

CleanVariable = System.Text.RegularExpressions.Regex.Replace(DirtyVariable, “[^a-z A-Z 0-9]”, “”)
image
This will replace all characters except these: (^a-z A-Z 0-9) with nothing (""). Update the brackets in the above as necessary.

AFTER:
CleanVariable = 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.
image

Some developers will clean all strings before using them.

Hopefully you found this post helpful. :blush:

BONUS SECTION: Celebrate you Regex Champion! :love_you_gesture: :champagne: :tophat:
Thanks for reading this far.

Please bookmark this post, show it some support by clicking the :heart: and share it with new users.

Feel free to message me :email: if you have a Regex problem but make sure you provide SOP as per Section :one: (Sample, Output, Pattern).
Also, don’t forget to subscribe to @AndersJensen on YouTube(https://www.youtube.com/user/klogeanders)! Anders has some really great content and is actively uploading HIGH quality tutorials each week.

Remember there are plenty of Regex Gurus out there! (I am a Regex Lover and wannabe guru :laughing:)

The gurus I see on the forums 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 gurus in my book :smiley:

31 Likes

@Steven_McKeering Really very helpful :hugs:

1 Like

@Steven_McKeering
thanks for this valuable post. Bringing the SOP Pattern into the picture is a big plus and lets accelerate the answering activities to the upcoming RegEx questions here in the forum.

:+1:

2 Likes

@Steven_McKeering

This is great! This will really help the community.

1 Like

Wow
This is phenomenal and many will find it helpful.

Cheers @Steven_McKeering

1 Like

Nicely done! Regexes can be difficult for people with and without developer backgrounds, and this is very helpful.

1 Like

Hi @Steven_McKeering

I found this really great. It is very helpful for all of us along with beginners. Really appreciate. :innocent: :hugs: :love_you_gesture:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

1 Like

@Steven_McKeering

Nice post, It’s really useful

Thanks

1 Like

@Steven_McKeering Awesome work. This post is a Gift to the Community now. All the required methods are presented in the best way possible to understand.

Yes, The SOP Pattern will bring a huge difference now and it will help the Community to provide a Solution really fast.

Thank you for providing such Great Informative Posts.
:smiley:

1 Like

@Steven_McKeering

Great post! This helps Increasing quality of regex beginner’s posts.

1 Like

@Steven_McKeering
Awesome work! Let me move it to the official FAQ section! I love it.

7 Likes

@Steven_McKeering
Thank you. Just the SOP alone is a great help but you also give a lot of good practical tools to begin right away. Good job.

1 Like

@Steven_McKeering
Great Work buddy, This will help a lot!
Thank You

1 Like

@Steven_McKeering

Awesome work :clap: :clap: It’s really helpful to us. I bookmarked this post and will share with other community members if they face any issues or queries related to RegEx.

2 Likes

Hey @Steven_McKeering

This is amazing work bro… im sure this will be super helpful for the guys here who look for regex solutions.

Thank you so much for putting it up and for all your amazing efforts my friend… this is super cool work… i just bookmarked it too so I can easily access…

Keep it up man!!!

1 Like

Its a wow article!! Cheers @Steven_McKeering

1 Like

Wow, amazing work @Steven_McKeering You are the best! I believe excellent Regex (and Excel/VBA) skills will bring you very far as an RPA developer.

2 Likes

Firstly, thank you everyone for your AMAZING :star_struck: response and support! :blush:

The secondary goal of this post was to make the forum ‘gurus’ lives easier when responding to Regex forum posts. Time will tell whether this was achieved :crossed_fingers:

I have made many edits (50+) to the post at this stage to try and improve it a little bit. Hopefully I can add a few more examples that people find friendly. Your suggestions are always welcome.

  • Thank you to Pablito and @bcorrea for updating the category post/tags.
  • Big thank you again to Pablito for the adding the hyperlinks in the contents section. :raised_hands:
  • Thanks @msan for the detailed feedback :sunglasses:

I hope one day I will be able to contribute another valuable post to this great community on a different topic. I appreciate your support.

Happy Automation!

P.S - Send new users to @AndersJensen’s YouTube Channel. Anders is building a dedicated playlist for a full UiPath Beginners Tutorial.

5 Likes

That is one hellava post, thank you VERY MUCH!!!

3 Likes

@Steven_McKeering, Whoa!

This is awesome dude, thank you.

2 Likes