What Is a Regular Expression?
A regular expression is like a smart search — it helps the system find patterns in text, not just exact words.
Think of it like telling the system:
"Look for anything that looks like this, not just exactly this."
Everyday Analogy:
Supermarket Example
You tell a store assistant:
“I want any fruit that starts with A and ends with E.”
Instead of checking every single word, the assistant understands the pattern and brings you:
-
Apple
-
Artichoke
That’s what regex does — it looks for matching patterns, not exact words.
Easy Examples
1. Find postal codes that start with "23"
Value: ^23
-
^
means “starts with” -
23
is the number you want
Matches:
-
23456
✅ -
23999
✅ -
12345
❌ (doesn’t start with 23)
2. Find postal codes that end with "89"
Value: 89$
-
$
means “ends with” -
89
is the number you want
Matches:
-
4789
✅ -
2289
✅ -
8923
❌ (doesn’t end with 89)
3. Match any number with exactly 5 digits (like a postal code)
Value: ^\d{5}$
-
\d
means any digit (0–9) -
{5}
means exactly 5 digits -
^
and$
mean “start and end”
Matches:
-
12345
✅ -
90210
✅ -
1234
❌ (only 4 digits) -
123456
❌ (too many digits)
4. Match any text that contains the word “Spain”
Value: Spain
Matches:
-
Order from Spain
✅ -
Shipped to Spain via DHL
✅ -
German order
❌
✅ This is the simplest use — just searching for the word.
5. Match emails
Value: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}
✅ Examples that Match the Pattern
Matches? | Why | |
---|---|---|
john.doe@example.com |
✅ | Standard email with dot in name |
test123@sub-domain.co.uk |
✅ | Includes subdomain and multiple parts |
name+tag@company.org |
✅ | Uses + symbol, which is valid |
user_01@mail-server.net |
✅ | Underscore _ and hyphen - are allowed |
Matches? | Why Not | |
---|---|---|
justtext |
❌ | Missing @ and domain |
user@.com |
❌ | Domain is missing before the dot |
user@site.c |
❌ | Domain extension only has 1 letter (needs 2 or more) |
@example.com |
❌ | No username before @ |
Postal Code example: ⬇️
1. Postal codes that start with 23
Value: ^23
What it means:
Symbol | Meaning |
---|---|
^ |
Start of the value (e.g., beginning of the postal code) |
23 |
The exact numbers we want at the start |
-
23000
✅ (starts with 23) -
23456
✅ -
23999
✅
Doesn’t match:
-
12345
❌ (doesn't start with 23)
2. Postal codes that end with 89
Value: 89$
What it means:
Symbol | Meaning |
---|---|
89 |
The exact numbers we want at the end |
$ |
End of the value (e.g., end of the postal code) |
Matches:
-
4789
✅ (ends with 89) -
9989
✅
Doesn’t match:
-
8912
❌ (89 is at the start, not the end) -
12345
❌
3. Exactly 5-digit postal codes
Value: ^\d{5}$
What it means:
Symbol | Meaning |
---|---|
^ |
Start of the value |
\d |
A single digit (0–9) |
{5} |
Exactly 5 of the thing before it (here: 5 digits) |
$ |
End of the value |
So it checks:
➤ “Starts with a digit, has exactly 5 digits, and nothing more after that.”
Matches:
-
12345
✅(start with a digit, it has only 5 digits, nothing more after that ) -
90210
✅
Doesn’t match:
-
1234
❌ (only 4 digits) -
123456
❌ (6 digits) -
12A45
❌ (contains a letter)
4. Postal codes that contain "00" anywhere
Value: 00
What it means:
Symbol | Meaning |
---|---|
00 |
Simply looks for the number sequence “00” anywhere in the value |
Matches:
-
10023
✅ -
23001
✅ -
80000
✅
Doesn’t match:
-
12345
❌ -
23456
❌
5. Postal codes that start with 2 and end with 6
Value: ^2.*6$
What it means:
Symbol | Meaning |
---|---|
^ |
Start of the value |
2 |
Must start with the number 2 |
.* |
Any number of characters (digits or letters) in between |
6 |
Ends with the number 6 |
$ |
End of the value |
.*
= Think of it as a “wildcard” — it means "any characters in the middle"
Matches:
-
2006
✅ -
23456
✅ 2v9456
✅-
298456
✅
Doesn’t match:
-
12346
❌ (starts with 1, not 2) -
23457
❌ (ends with 7, not 6)
6. Postal codes with two values in one field
Value: ^(?:(06039|06059))$
This is a regular expression used to match specific postal codes — in this case, either 06039
or 06059
.
^
-> The beginning of the string. It means the match must start at the very beginning.
(?:..)
-> A non-capturing group. This groups options without saving them for later. Think of it as a simple container.
06039|06059
-> Inside the group, the **pipe (`
$
-> The end of the string. It means the match must end here, with nothing after.
Summary of Common Symbols
Symbol | Meaning |
---|---|
^ |
Start of the value (e.g., the beginning of a postal code) |
$ |
End of the value |
. |
Any one character |
* |
Zero or more of the previous thing (used with . in .* ) |
\d |
A digit (0–9) |
{5} |
Exactly 5 of the thing before it |