Back 🎯 REGEX (Regular Expressions) 02 Feb, 2026

🧩 “Pattern Language to Find, Extract & Validate Text”

Regex = a smart search pattern
Instead of searching exact words, you search rules


🧠 Simple Analogy (Layman First)

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


🟦 BASIC BUILDING BLOCKS (Foundation)


🔹 1️⃣ Literal Characters

Meaning

Match exact text

apple

Matches:

apple
pineapple

❌ Does NOT match:

apples

🧠 Think: Normal search


🔹 2️⃣ Dot .Any One Character

a.p

Matches:

app
aap
alp

❌ Not match:

ap

🧠 Dot = “kuch bhi chalega (single char)”


🟩 CHARACTER CLASSES (Choices)


🔹 3️⃣ [abc] → Any ONE of these

[aeiou]

Matches:

a e i o u

🧠 “Pick any one from this basket”


Range

[a-z]
[A-Z]
[0-9]

Example:

[0-9]

Matches:

1 5 9

❌ Negation

[^0-9]

Matches:

a @ #

🧠 ^ inside [] = “except this”


🟨 SHORTCUT CHARACTERS (Very Important ⭐)

RegexMeaning
\dDigit (0–9)
\DNot digit
\wWord char (a-z A-Z 0-9 _)
\WNot word
\sSpace
\SNot space

Example:

\d\d\d

Matches:

123

🟧 QUANTIFIERS (How many times?)

This is where real power starts 🔥


🔹 4️⃣ * → 0 or more

a*

Matches:

""  a  aa  aaa

🧠 “Ho bhi sakta hai, nahi bhi”


🔹 5️⃣ + → 1 or more ⭐

a+

Matches:

a  aa  aaa

❌ Not match:

""

🧠 “Kam se kam ek baar”


🔹 6️⃣ ? → 0 or 1

colou?r

Matches:

color
colour

🧠 Optional letter


🔹 7️⃣ {n}, {n,m} → Exact / Range

\d{10}

Matches:

9876543210
\d{2,4}

Matches:

12 123 1234

🟥 ANCHORS (Position Control)


🔹 8️⃣ ^ → Start of string

^Hello

Matches:

Hello world

❌ Not:

Say Hello

🔹 9️⃣ $ → End of string

world$

Matches:

Hello world

🟪 GROUPING & EXTRACTION (MOST USED IN SCRAPING)


🔹 🔟 ( ) → Capture Group ⭐⭐⭐

Price: ₹(\d+)

Text:

Price: ₹14999

Captured:

14999

🧠 “Bracket ke andar wala extract karo”


Example (Flipkart reviews)

(\d+(,\d+)?)\s+Reviews

Matches:

1,234 Reviews
234 Reviews

Captured:

1,234
234

🔹 Non-capturing group

(?:abc)

Used when grouping needed but no extraction


🟫 OR CONDITION (Either / Or)


🔹 1️⃣1️⃣ | → OR

cat|dog

Matches:

cat
dog

🧠 “ya to ye, ya wo”


🧪 REAL SCRAPING EXAMPLES


✔ Extract product ID

/p/(itm[0-9A-Za-z]+)

URL:

/apple-iphone/p/itmABC123

Result:

itmABC123

✔ Extract price

₹\s?(\d+(,\d+)?)

Matches:

₹14,999
₹999

✔ Extract rating

(\d\.\d)\s★

Matches:

4.3 ★

🎨 VISUAL SUMMARY CHEAT MAP

\d       → digit
\w       → word
+        → one or more
*        → zero or more
?        → optional
{n,m}    → range
()       → extract
|        → OR
^ $      → start / end

🧠 GOLDEN RULES FOR REGEX (Remember This)

✅ Always test regex online
✅ Start simple → then refine
❌ Don’t over-optimize early
✅ Regex reads left to right


🔧 Best Tool for Practice

👉 regex101.com
(Shows matches, groups, explanation)


🎯 One-Line Memory Trick

Regex = Smart CTRL + F with brain