Intermediate·9 min·intermediate · text
Regular Expressions
Regular Expressions
A pattern language for text. Python's re module:
Three workhorses
re.findall(pattern, text)— every non-overlapping match, as a listre.search(pattern, text)— first match anywhere, orNonere.sub(pattern, repl, text)— replace matches
Cheat sheet
\ddigit ·\wword char ·\swhitespace+one or more ·*zero or more ·?optional( )capture group ·[abc]any of a/b/c
Use raw strings
Always prefix patterns with r to avoid backslash hell: r"\d+".
Try it
- Extract all numbers from
"order #1234, total ₹5,600". - Replace double-spaces with single in
"a b c".