1.3.11 · HinglishPython Intermediate

Regular expressions — re module, patterns, groups, findall, sub

1,600 words7 min readRead in English

1.3.11 · Coding › Python Intermediate


WHY regexes exist karte hain?

Mental model:

  • Tum ek pattern dete ho (ek shape ki description).
  • Engine ek cursor left→right move karta hai aur poochta hai "kya text yahan se shuru hokar meri shape se match karta hai?"
  • Woh matches report karta hai, optionally groups (captured sub-pieces) bhi nikalta hai.

WHAT hain building blocks?


HOW 5 key functions alag hain


Raw-string ka WHY


Figure — Regular expressions — re module, patterns, groups, findall, sub

Worked examples



Reuse ke liye Compiling


Recall Feynman: ek 12-saal ke bacche ko samjhao

Socho tumhare paas ek badi kitaab hai aur tum har phone number highlight karna chahte ho. Har line khud padhne ke bajaye, tum ek robot ko ek rule card dete ho: "woh chunk dhundho jo kuch digits, ek dash, aur kuch aur digits jaisa dikhta ho." Robot zoom karke har woh chunk highlight kar deta hai jo card se match kare. Regex woh rule card hai. ( ) matlab tum card par chhoti boxes bana rahe ho aur bol rahe ho "aur is box ke andar jo hai woh alag yaad rakhna." findall = "mujhe har highlight dikhao." sub = "har highlight ko is naye text se replace karo."


Flashcards

Kuch match na hone par re.search kya return karta hai?
None (toh .group call karne se pehle check karna zaroori hai).
re.match aur re.search mein kya fark hai?
match sirf string ke start par check karta hai; search kahin bhi scan karta hai.
\d+ ka matlab kya hai?
Lagataar ek ya zyada digits.
Patterns ke liye raw strings r"..." kyun use karte hain?
Taaki Python backslash na khaaye; regex engine ko \d, \b etc. intact mile.
Jab pattern mein 2 capturing groups hों toh findall kya return karta hai?
Tuples ki ek list, har match ke liye group strings ka ek tuple.
group(0) kya hai?
Poora matched substring.
Quantifier ko lazy kaise banate hain?
Uske baad ? lagao (jaise .*?), taaki woh jitne kam chars ho sake match kare.
re.sub mein replacement string mein \1 ka kya matlab hai?
Group 1 dwara captured text (ek backreference).
Non-capturing group kya hai aur kyun use karte hain?
(?:...) structure/alternation ke liye group karta hai bina use numbered group ke roop mein store kiye.
^ aur $ kya anchor karte hain?
^ = string ka start, $ = string ka end.
re.match("dog","the dog") None kyun hai?
match position 0 par anchored hai; "dog" start mein nahi hai.
re.compile se kya fayda hota hai?
Pattern ko ek baar parse karke reusable object banata hai — loops mein faster.

Connections

  • String methods — split, join, format
  • Python Intermediate — File I/O and parsing logs
  • Finite State Machines (regex engines under the hood NFAs/DFAs hote hain)
  • Greedy vs Backtracking algorithms
  • Escape characters and raw strings

Concept Map

describes shape of

built into

repeat previous atom

capture sub-pieces

used by

used by

used by

used by

first match anywhere

only at start pos 0

list of all matches

replaces matches

passes backslash through

findall returns tuples via

Regex pattern language

Regex engine scans text

Pattern atoms

Quantifiers

Capturing groups

re.search

re.match

re.findall

re.sub

Raw strings r-prefix