🧩 “Pattern Language to Find, Extract & Validate Text”
Regex = a smart search pattern
Instead of searching exact words, you search rules
Imagine you say:
“Bring me any mobile number from this text”
You don’t know the number, but you know the pattern:
10 digits
Starts with 6–9
👉 Regex does exactly this
Match exact text
apple
Matches:
apple
pineapple
❌ Does NOT match:
apples
🧠 Think: Normal search
. → Any One Charactera.p
Matches:
app
aap
alp
❌ Not match:
ap
🧠 Dot = “kuch bhi chalega (single char)”
[abc] → Any ONE of these[aeiou]
Matches:
a e i o u
🧠 “Pick any one from this basket”
[a-z]
[A-Z]
[0-9]
Example:
[0-9]
Matches:
1 5 9
[^0-9]
Matches:
a @ #
🧠 ^ inside [] = “except this”
| Regex | Meaning |
|---|---|
\d | Digit (0–9) |
\D | Not digit |
\w | Word char (a-z A-Z 0-9 _) |
\W | Not word |
\s | Space |
\S | Not space |
Example:
\d\d\d
Matches:
123
This is where real power starts 🔥
* → 0 or morea*
Matches:
"" a aa aaa
🧠 “Ho bhi sakta hai, nahi bhi”
+ → 1 or more ⭐a+
Matches:
a aa aaa
❌ Not match:
""
🧠 “Kam se kam ek baar”
? → 0 or 1colou?r
Matches:
color
colour
🧠 Optional letter
{n}, {n,m} → Exact / Range\d{10}
Matches:
9876543210
\d{2,4}
Matches:
12 123 1234
^ → Start of string^Hello
Matches:
Hello world
❌ Not:
Say Hello
$ → End of stringworld$
Matches:
Hello world
( ) → Capture Group ⭐⭐⭐Price: ₹(\d+)
Text:
Price: ₹14999
Captured:
14999
🧠 “Bracket ke andar wala extract karo”
(\d+(,\d+)?)\s+Reviews
Matches:
1,234 Reviews
234 Reviews
Captured:
1,234
234
(?:abc)
Used when grouping needed but no extraction
| → ORcat|dog
Matches:
cat
dog
🧠 “ya to ye, ya wo”
/p/(itm[0-9A-Za-z]+)
URL:
/apple-iphone/p/itmABC123
Result:
itmABC123
₹\s?(\d+(,\d+)?)
Matches:
₹14,999
₹999
(\d\.\d)\s★
Matches:
4.3 ★
\d → digit
\w → word
+ → one or more
* → zero or more
? → optional
{n,m} → range
() → extract
| → OR
^ $ → start / end
✅ Always test regex online
✅ Start simple → then refine
❌ Don’t over-optimize early
✅ Regex reads left to right
👉 regex101.com
(Shows matches, groups, explanation)
Regex = Smart CTRL + F with brain