
A regular expression is a search pattern. Instead of finding the text “INV-1042”, you find anything that looks like an invoice number.
In this article
The building blocks
| Symbol | Means |
|---|---|
\d |
a digit |
\w |
letter, digit or underscore |
\s |
space or tab |
. |
any character |
+ * ? |
one or more / zero or more / optional |
{3} {2,4} |
exactly 3 / between 2 and 4 |
[A-Z] |
one capital letter |
^ $ |
start / end of the text |
( ) |
group (capture a part) |
10 patterns
| Find | Pattern |
|---|---|
| Invoice numbers like INV-1042 | INV-\d+ |
| Indian mobile | [6-9]\d{9} |
| PAN | [A-Z]{5}\d{4}[A-Z] |
| PIN code | \b\d{6}\b |
| Email (simple) | [\w.+-]+@[\w-]+\.[\w.]+ |
| Date dd/mm/yyyy | \d{2}/\d{2}/\d{4} |
| Amount with commas | \d{1,3}(,\d{2,3})+(\.\d+)? |
| Double spaces | \s{2,} |
| Lines starting with # | ^#.* |
| GSTIN | \d{2}[A-Z]{5}\d{4}[A-Z]\d[Z][A-Z\d] |
Using it in VBA
“`visual-basic
Dim re As Object: Set re = CreateObject(“VBScript.RegExp”)
re.Pattern = “[6-9]\d{9}”: re.Global = True
If re.Test(Range(“A2”).Value) Then Range(“B2”).Value = re.Execute(Range(“A2”).Value)(0)
“`
💡 Test patterns interactively in the free Regex Tester before using them on real data.
✨ Ask AI about this article
Stuck on a step? Ask a question and the AI answers using this article.
Free · AI can be wrong