Regex in Power Automate Desktop: A Practical Guide to Capturing Text
Regular Expressions, commonly known as Regex, are extremely useful when working with text in Power Automate Desktop (PAD). They allow you to find, identify, and extract specific information from unstructured text such as PDF documents, invoices, bank statements, emails, reports, and forms.
In this guide, we will cover the most commonly used Regex patterns for Power Automate Desktop, with practical examples that can be directly applied to automation projects.
http://regexstorm.net/tester
What is Regex?
A Regular Expression is a pattern used to search for specific text or values within a larger block of text.
For example, suppose a PDF contains:
Customer ID: XXXXX1234
Account No: 99991XXXXX10099
PAN: 12XXXXXXXKInstead of manually searching for these values, Regex can automatically identify and extract them.
For example:
Customer\s+ID\s*:\s*(\S+)returns:
XXXXX1234Similarly:
Account\s+No\.?\s*:\s*([A-Za-z0-9]+)returns:
99991XXXXX10099Why Regex is Useful in Power Automate Desktop
When PAD extracts text from a PDF, the resulting text may not have exactly the same formatting as the original document.
For example, the PDF may visually display:
Account No. Branch IFSC Code
99991XXXXX10099 KUKATPALLY IFSC000099PAD might extract it as:
Account No. Branch IFSC Code
99991XXXXX10099 KUKATPALLY IFSC000099There may be:
Multiple spaces
Tabs
Line breaks
Unexpected formatting
Text spread across multiple lines
Regex allows us to create patterns that are flexible enough to handle these differences.
1. Basic Regex Characters
These are the fundamental Regex characters you should understand.
| Regex | Meaning | Example |
|---|---|---|
. | Any single character | A, 1, @ |
\d | Any digit | 5 |
\D | Any non-digit | A |
\w | Letter, number, or _ | Account_1 |
\W | Non-word character | @, - |
\s | Whitespace | Space, tab, newline |
\S | Non-whitespace | Account |
2. Matching Numbers
The \d pattern is commonly used when extracting numbers.
One digit
\dMatches:
5One or more digits
\d+Matches:
123456Exactly 10 digits
\d{10}Matches:
9876543210Between 6 and 10 digits
\d{6,10}Matches numbers containing between 6 and 10 digits.
3. Matching Letters and Numbers
Use character classes when you know what characters are allowed.
Uppercase letters
[A-Z]Lowercase letters
[a-z]Any English letter
[A-Za-z]Letters or numbers
[A-Za-z0-9]One or more letters or numbers
[A-Za-z0-9]+This is particularly useful for values such as:
99991XXXXX10099
12XXXXXXXK
ABC1234564. Understanding \s+
One of the most useful Regex patterns for PDF automation is:
\s+It means:
One or more whitespace characters.
Whitespace can include:
Spaces
Tabs
New lines
This is important because PDF extraction may convert visual spacing into tabs or multiple spaces.
For example, all of these can potentially be handled:
Account No.Account No.Account No.Therefore, instead of:
Account Noit is usually better to use:
Account\s+Nowhen processing extracted PDF text.
5. Optional Characters Using ?
The ? quantifier means that the preceding element is optional.
For example:
No\.?can match:
Noand:
No.This is useful when document formatting varies.
Another common example:
https?://matches both:
http://and:
https://6. Quantifiers
Quantifiers control how many times a pattern can occur.
| Regex | Meaning |
* | Zero or more |
+ | One or more |
? | Zero or one |
{3} | Exactly 3 |
{3,} | 3 or more |
{3,10} | Between 3 and 10 |
Examples
\d*Zero or more digits.
\d+One or more digits.
\d{6}Exactly six digits.
\d{6,10}Between six and ten digits.
7. Capture Groups
Capture groups are one of the most important concepts when extracting values.
Create a capture group by placing the desired pattern inside parentheses:
(...)For example:
Account No. 99991XXXXX10099Use:
Account\s+No\.\s+([A-Za-z0-9]+)The entire match may be:
Account No. 99991XXXXX10099But the value inside the capture group:
([A-Za-z0-9]+)is:
99991XXXXX10099This is exactly what we normally want in an automation.
8. Multiple Capture Groups
You can create multiple groups in the same expression.
For example:
Customer Name: Ramesh Beerla
Account No: 99991XXXXX10099Regex:
Customer\s+Name:\s*(.+)\r?\nAccount\s+No\.?\s*:?\s*([A-Za-z0-9]+)Group 1:
Ramesh BeerlaGroup 2:
99991XXXXX10099This allows a single Regex pattern to extract multiple related values.
9. Capturing Text After a Label
This is one of the most common Regex requirements in PAD.
Suppose the document contains:
Customer ID: XXXXX1234Use:
Customer\s+ID\s*:\s*(\S+)Result:
XXXXX1234For:
PAN: 12XXXXXXXKuse:
PAN\s*:\s*(\S+)Result:
12XXXXXXXK10. Capturing Text Until the End of the Line
Use:
(.+)For example:
Customer Name: Ramesh BeerlaRegex:
Customer\s+Name:\s*(.+)The captured value is:
Ramesh BeerlaThis is useful when the value can contain spaces.
For example, \S+ would capture only the first word:
Rameshwhile:
(.+)can capture:
Ramesh Beerla11. Capturing a Single Value
When the value doesn't contain spaces, use:
(\S+)For example:
PAN: 12XXXXXXXKRegex:
PAN:\s*(\S+)Result:
12XXXXXXXKThis is often simpler than using [A-Za-z0-9]+.
12. Matching Anything Using .
The dot:
.matches almost any single character.
For example:
A.Bcan match:
A1B
A-B
A Bbecause the middle character can be different.
13. .* and .*?
These patterns are very useful when working with PDFs.
.*
.*means:
Match zero or more characters.
It is greedy, meaning it tries to match as much as possible.
.*?
.*?is non-greedy, meaning it tries to match as little as possible.
For document extraction, .*? is often safer.
For example:
Account.*?Balancefinds the shortest section between:
Accountand:
Balance14. Matching a New Line
PDF extraction frequently involves line breaks.
Use:
\nfor a new line.
For Windows-style line breaks, use:
\r\nA flexible option is:
\r?\nThis handles both common formats.
For example:
Account No. Branch
99991XXXXX10099can be captured using:
Account\s+No\.\s+Branch.*?\r?\n([A-Za-z0-9]+)The capture group returns:
99991XXXXX1009915. Matching Anything Except a Character
Use the ^ inside a character class.
For example:
[^,]+means:
Match one or more characters that are not commas.
For:
Ramesh, Hyderabad, Indiathis can capture:
RameshSimilarly:
[^:]+captures everything before a colon.
16. OR Conditions
Use:
|to represent OR.
For example:
Account|Customer|Clientmatches any of:
Account
Customer
ClientYou can combine this with groups:
Account\s+(No\.|Number)This can match:
Account No.or:
Account Number17. Word Boundary
Use:
\bto specify a word boundary.
For example:
\bAccount\bwill match:
Accountbut not necessarily:
AccountNumberAnother example:
\b\d{6}\bcan be used to find a standalone six-digit number.
18. Extracting Text Between Two Values
Suppose the PDF contains:
Account No. 99991XXXXX10099 for the periodUse:
Account\s+No\.\s+(.+?)\s+forThe capture group returns:
99991XXXXX10099The ? makes the match non-greedy so it stops at the first occurrence of for.
19. Common Patterns for PDF Automation
Here are some practical patterns you can reuse.
Label followed by a value
Label\s*[:\-]?\s*(\S+)Label followed by text
Label\s*[:\-]?\s*(.+)Label followed by numbers
Label\s*[:\-]?\s*(\d+)Value on the next line
Label.*?\r?\n\s*(\S+)Text between two labels
Label1\s*(.*?)\s*Label220. Common Business Examples
Account Number
Account\s+(?:No\.|Number)\s*[:\-]?\s*([A-Za-z0-9]+)Example:
Account No: 99991XXXXX10099Result:
99991XXXXX10099Customer ID
Customer\s+ID\s*[:\-]?\s*(\S+)PAN
PAN\s*[:\-]?\s*([A-Z0-9]+)Indian PIN Code
\b\d{6}\bMobile Number
\b\d{10}\bDate — DD-MM-YYYY
\b\d{2}-\d{2}-\d{4}\bDate — DD/MM/YYYY
\b\d{2}/\d{2}/\d{4}\bEmail Address
[\w.+-]+@[\w-]+\.[\w.-]+URL
https?://\S+Decimal Amount
\d+(?:\.\d{2})?Matches:
123
123.45Indian Currency Format
For an amount such as:
11,62,048.27use:
\d{1,3}(?:,\d{2,3})*(?:\.\d{2})?21. A Real Power Automate Desktop Example
Suppose PAD extracts the following from a bank statement:
Customer ID: XXXXX1234
PAN: 12XXXXXXXK
Savings Account(s)
Summary
Account No. Branch IFSC Code MICR Code CRN Balance Type
99991XXXXX10099 KUKATPALLY, HYDERABAD IFSC000099
Statement for Account No. 99991XXXXX10099 for the periodWe can extract the Account Number using:
Statement\s+for\s+Account\s+No\.\s+([A-Za-z0-9]+)The important part is:
([A-Za-z0-9]+)This is Capture Group 1.
The resulting value is:
99991XXXXX10099This approach is better than relying on the account number always being on a particular line because PDF extraction can change line breaks and spacing.
22. Recommended Regex Pattern for PAD
For most label/value extraction scenarios, start with this structure:
Label\s*[:\-]?\s*(VALUE)Replace VALUE depending on what you're capturing.
Numbers
Label\s*[:\-]?\s*(\d+)Letters and numbers
Label\s*[:\-]?\s*([A-Za-z0-9]+)A value without spaces
Label\s*[:\-]?\s*(\S+)Text containing spaces
Label\s*[:\-]?\s*(.+)This simple pattern can solve a large percentage of text-extraction requirements in Power Automate Desktop.
23. Regex Cheat Sheet
| Requirement | Regex |
| One digit | \d |
| Number | \d+ |
| Exactly 6 digits | \d{6} |
| Letter | [A-Za-z] |
| Letter/number | [A-Za-z0-9]+ |
| Non-space value | \S+ |
| Whitespace | \s |
| Multiple spaces/tabs | \s+ |
| Optional spaces | \s* |
| Any character | . |
| Any text | .* |
| Minimal text | .*? |
| New line | \r?\n |
| Capture value | (VALUE) |
| OR | A|B |
| Word boundary | \b |
| Anything except comma | [^,]+ |
| Start of line | ^ |
| End of line | $ |
| Optional character | ? |
Best Practices for Regex in Power Automate Desktop
1. Don't depend on exact spacing
Avoid:
Account No.Prefer:
Account\s+No\.2. Use capture groups
If you need only the value, put it inside:
(...)3. Use labels whenever possible
Instead of extracting the 43rd line, prefer:
Account\s+No\.\s+([A-Za-z0-9]+)This makes your automation more resilient when PDF layouts change.
4. Test with the actual extracted PDF text
Don't test Regex only against what the PDF visually looks like. Test it against the actual value stored in the PAD variable after Extract text from PDF.
5. Prefer specific patterns
Instead of:
\d+which may return many numbers, use:
Account\s+No\.\s+(\d+)when the account number follows a known label.
Final Takeaway
You don't need to memorize hundreds of Regex patterns.
For Power Automate Desktop, focus on these building blocks:
\s+Whitespace
\d+Numbers
\S+Single non-space value
[A-Za-z0-9]+Letters and numbers
(.+)Text including spaces
(.*?)Minimal text capture
\r?\nNew line
(...)Capture group
Once you understand these, you can combine them to extract almost any structured value from PDF text.
For example:
Statement\s+for\s+Account\s+No\.\s+([A-Za-z0-9]+)can extract the account number from:
Statement for Account No. 99991XXXXX10099 for the period...and return:
99991XXXXX10099That combination of stable labels + flexible whitespace + capture groups is one of the most useful Regex techniques for Power Automate Desktop automation.
Comments
Post a Comment