Essential Regex Mastery in Automation with Real-World Examples

Regex, short for Regular Expression, is a pattern-matching technique used to search, identify, validate, extract, or manipulate text.

Rather than matching one exact text, it describes what the text should look like.

Regex in Automation helps extract needed values, such as INV-90378389, from invoices stored as semi-structured or unstructured text.

Character Classes

  • [abc] – Matches specific characters
  • [^abc] – Start’s with Letter
  • [a-z] – All lower case letters
  • [A-Z] – All upper case letters
  • [0-9] – All digits
  • [a-zA-Z0-9] – All letters Lower and Upper case and Digits except symbols

Predefined Character Classes

  • \d — digit
  • \D — non-digit
  • \w — word character
  • \W — non-word character
  • \s — whitespace
  • \S — non-white-space
  • . — any character(Single)

Quantifiers

  • * – 0 or More
  • + – 1 or More
  • ? – 0 or 1
  • {n} – specific number
  • {n,} – from specific number to unlimited
  • {n,m} – between 2 number Like 3 to 6, 8 to 20

Anchors

  • ^ – word Starts with
  • $ – word End’s with
  • \b – Word Boundary : (\cat\b) selects cat only , but Not catalog
  • \B – Not a Word Boundary : (\cat) selects catalog, but Not cat

Groups

  • ( )
  • Capturing groups
  • Multiple groups
  • Nested groups
  • Non-capturing groups (?:...)

Alternation

  • |OR Multiple patterns ( @gmail.com|@yahoo.com|@outlook.com)

workarounds

  • Positive lookahead (?=USD)USD Followed by or before or Front of a particular pattern [100 USD]
  • Negative lookahead (?!EUR)not EUR Followed by or before or Front of a particular pattern [100 USD]
  • Positive lookbehind (?<=USD)USD Preceded or after or Back of particular pattern [USD 100]
  • Negative lookbehind (?<!EUR)not EUR Preceded or after or Back of particular pattern [100 USD ]

Greedy vs Lazy Matching

  • Greedy .*
  • Lazy .*?
  • Why greedy matching causes extraction problems
  • Real invoice example

Common Regex Patterns

Regex is useful for extracting structured information from unstructured text. Common automation scenarios include extracting emails such as support@example.com, phone numbers such as +91 9876543210, URLs such as https://myautomationtime.com, dates such as 11/08/2026, times such as 10:30 PM, IP addresses such as 192.168.1.100, numbers such as 125, decimal values such as 1250.75, currency values such as ₹12,500.50, PAN/GST identifiers such as ABCDE1234F and 29ABCDE1234F1Z5, and invoice numbers such as INV-2026-00125. These patterns are especially useful in RPA for processing invoices, emails, SAP data, logs, reports, and other unstructured text.
  • Email – [\w.-]+@[\w.-]+\.\w+
  • Phone number – \+?\d{1,3}[\s-]?\d{10}
  • URL – https?://[^\s]+
  • Date – \b\d{2}/\d{2}/\d{4}\b
  • Time – \b\d{1,2}:\d{2}\s?(?:AM|PM)\b
  • IP address – \b(?:\d{1,3}\.){3}\d{1,3}\b
  • Numbers – \b\d+\b
  • Decimal values – \b\d+\.\d+\b
  • Currency – (?:₹|Rs\.?|\$)\s?[\d,]+(?:\.\d{2})?
  • PAN -[A-Z]{5}\d{4}[A-Z]\b
  • GSTIN – \b\d{2}[A-Z]{5}\d{4}[A-Z]\d[Z][A-Z0-9]\b
  • Invoice numbers – \bINV[-/]\d{4}[-/]\d+\b

Regex Flags / Options

  • Ignore case
  • Multiline
  • Single-line / DotAll
  • Global matching
  • Culture considerations

Regex with UiPath

  • Regex.Match
  • Regex.Matches
  • Regex.Replace
  • Regex.Split
  • Regex.IsMatch

Common Regex Mistakes

  • Using .* everywhere
  • Forgetting to escape special characters
  • Incorrect anchors
  • Overly complicated expressions
  • Not considering multiline text
  • Capturing when non-capturing groups are better
  • Using regex when a simple string operation is better

    Regex Challenge

    send your solution : myautomationtime@gmail.com or surya@myatomationtimes.com

    Can you master Regex?

    Test your pattern-matching skills in this exciting challenge.

    It covers real-world text extraction tasks from basic to advanced levels.

    Participants who finish these challenges with accurate and optimized Regex patterns can win exciting gifts and recognition.

    Challenge Rules:

    • Solve the problems using Regex only.
    • Any programming language or automation tool can be used.
    • Share your Regex pattern and extracted output.
    • Bonus points for optimized and readable expressions.

    Special Reward: A surprise gift will be provided to selected winners based on correctness, creativity, and efficiency.

    Challenge Problems

    Beginner Level

    1. Email Extractor

    Extract all email addresses from the text.

    Contact us at support@example.com, admin@company.org, and help123@gmail.com
    

    Expected Output:

    support@example.com
    admin@company.org
    help123@gmail.com
    

    2. Phone Number Finder

    Extract all Indian mobile numbers.

    Call +91 9876543210 or 9123456789 for support.
    

    Expected Output:

    9876543210
    9123456789
    

    3. Date Extractor

    Extract all dates in DD/MM/YYYY format.

    Invoice Date: 11/08/2026, Due Date: 25/08/2026
    

    Expected Output:

    11/08/2026
    25/08/2026
    

    Intermediate Level

    4. Invoice Number Extractor

    Extract invoice numbers.

    Invoice No: INV-2026-00125
    Reference: INV-2026-00456
    

    Expected Output:

    INV-2026-00125
    INV-2026-00456
    

    5. Currency and Amount Extractor

    Extract currency values.

    ₹12,500.50
    USD 2500
    EUR 350.75
    

    Expected Output:

    ₹12,500.50
    USD 2500
    EUR 350.75
    

    6. PAN and GSTIN Extractor

    Extract PAN and GST numbers.

    PAN: ABCDE1234F
    GSTIN: 29ABCDE1234F1Z5
    

    Expected Output:

    ABCDE1234F
    29ABCDE1234F1Z5
    

    Advanced Level

    7. Log File Analyzer

    Extract only ERROR messages.

    INFO: Process Started
    ERROR: Database Connection Failed
    WARNING: Retry Attempt
    ERROR: File Not Found
    

    Expected Output:

    Database Connection Failed
    File Not Found
    

    8. Invoice Data Extraction Challenge

    Extract:

    • Invoice Number
    • Invoice Date
    • Total Amount
    Invoice No: INV-2026-00125
    Date: 11/08/2026
    Total: USD 1,250.50
    

    Expected Output:

    INV-2026-00125
    11/08/2026
    USD 1,250.50
    

    Ready to accept the challenge? Share your Regex solutions and compete with fellow automation enthusiasts. The best submissions will receive recognition!

    send your solution : myautomationtime@gmail.com or surya@myatomationtimes.com

    Leave a Reply