search() vs findall() vs finditer()Think of regex like a detective 🔎 searching inside a paragraph.
re.search() → Find FIRST match only“Mujhe pehla match dikhao, bas!”
re.search(pattern, text)
✔ Match object
❌ None if not found
text = "Price ₹999, Discount ₹100"
m = re.search(r"₹\d+", text)
₹999
m.group()
You need only one value
Product ID
First price
First email
🧠 Fast & simple
re.findall() → Find ALL matches (List)“Mujhe sab kuch chahiye 😄”
re.findall(pattern, text)
✔ List of matches
✔ Empty list [] if nothing found
text = "₹999 ₹1499 ₹1999"
prices = re.findall(r"₹\d+", text)
['₹999', '₹1499', '₹1999']
If capturing groups exist ( ), only group content is returned:
re.findall(r"₹(\d+)", text)
Result:
['999', '1499', '1999']
Multiple prices
All emails
All phone numbers
Review counts
🧠 Most used in scraping
re.finditer() → Iterator of match objects (PRO MODE 🚀)“Sab matches chahiye, details ke saath”
re.finditer(pattern, text)
✔ Iterator of match objects
✔ Memory efficient
text = "₹999 ₹1499"
for m in re.finditer(r"₹\d+", text):
print(m.group(), m.start(), m.end())
₹999 0 4
₹1499 5 10
You get:
.group()
.start()
.end()
.span()
Large text
Need positions
Advanced scraping
NLP pipelines
🧠 Most powerful & clean
| Feature | search() | findall() | finditer() |
|---|---|---|---|
| Finds | First only | All | All |
| Return type | Match / None | List | Iterator |
| Groups info | ✔ Yes | ❌ No | ✔ Yes |
| Position info | ✔ Yes | ❌ No | ✔ Yes |
| Memory usage | Low | Medium | Lowest |
| Beginner friendly | ⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐ |
Need one thing? → search()
Need many values? → findall()
Need details? → finditer()
re.search(r"/p/(itm\w+)", url)
re.findall(r"₹\d+", page_text)
re.finditer(r"₹\d+", page_text)
❌ Using findall() when only one value needed
❌ Forgetting groups change findall() output
❌ Ignoring None check in search()
✅ Always do:
if m:
print(m.group())
🔍 search → pehla
📋 findall → sab
🔁 finditer → sab + details