Back 🔍 search() vs findall() vs finditer() : Regex 02 Feb, 2026

🔍 search() vs findall() vs finditer()

🧠 Three ways Regex looks inside text

Think of regex like a detective 🔎 searching inside a paragraph.


🟥 1️⃣ re.search()Find FIRST match only

🧠 Think like

“Mujhe pehla match dikhao, bas!”


✨ Syntax

re.search(pattern, text)

📦 Returns

  • Match object

  • None if not found


🧪 Example

text = "Price ₹999, Discount ₹100"
m = re.search(r"₹\d+", text)

📤 Result

₹999

🔍 Access data

m.group()

✅ Best Use Case

  • You need only one value

  • Product ID

  • First price

  • First email

🧠 Fast & simple


🟦 2️⃣ re.findall()Find ALL matches (List)

🧠 Think like

“Mujhe sab kuch chahiye 😄”


✨ Syntax

re.findall(pattern, text)

📦 Returns

  • List of matches

  • ✔ Empty list [] if nothing found


🧪 Example

text = "₹999 ₹1499 ₹1999"
prices = re.findall(r"₹\d+", text)

📤 Result

['₹999', '₹1499', '₹1999']

⚠️ Important Behavior

If capturing groups exist ( ), only group content is returned:

re.findall(r"₹(\d+)", text)

Result:

['999', '1499', '1999']

✅ Best Use Case

  • Multiple prices

  • All emails

  • All phone numbers

  • Review counts

🧠 Most used in scraping


🟩 3️⃣ re.finditer()Iterator of match objects (PRO MODE 🚀)

🧠 Think like

“Sab matches chahiye, details ke saath


✨ Syntax

re.finditer(pattern, text)

📦 Returns

  • Iterator of match objects

  • ✔ Memory efficient


🧪 Example

text = "₹999 ₹1499"
for m in re.finditer(r"₹\d+", text):
    print(m.group(), m.start(), m.end())

📤 Output

₹999 0 4
₹1499 5 10

🔍 Extra Powers

You get:

  • .group()

  • .start()

  • .end()

  • .span()


✅ Best Use Case

  • Large text

  • Need positions

  • Advanced scraping

  • NLP pipelines

🧠 Most powerful & clean


🎨 VISUAL COMPARISON TABLE

Featuresearch()findall()finditer()
FindsFirst onlyAllAll
Return typeMatch / NoneListIterator
Groups info✔ Yes❌ No✔ Yes
Position info✔ Yes❌ No✔ Yes
Memory usageLowMediumLowest
Beginner friendly⭐⭐⭐⭐⭐⭐⭐⭐⭐

🧠 HOW TO CHOOSE? (Golden Rule)

Need one thing?      → search()
Need many values?    → findall()
Need details?        → finditer()

🧪 Flipkart Example (Real Life)

Extract product ID (single)

re.search(r"/p/(itm\w+)", url)

Extract all prices

re.findall(r"₹\d+", page_text)

Extract prices + position

re.finditer(r"₹\d+", page_text)

⚠️ COMMON BEGINNER MISTAKES

❌ Using findall() when only one value needed
❌ Forgetting groups change findall() output
❌ Ignoring None check in search()

✅ Always do:

if m:
    print(m.group())

🎯 ONE-LINE MEMORY TRICK 🧠

🔍 searchpehla
📋 findallsab
🔁 finditersab + details