Learn regular expressions by seeing patterns, test cases, and match results side-by-side
Use alternation and lookaround assertions for complex matching
The pipe | creates alternation, matching one pattern OR another.
Pattern breakdown:
\b(cat|dog|bird)\b - match "cat" OR "dog" OR "bird" as whole wordsThis is useful for matching multiple alternatives in one pattern.
I have a cat and a dog. Birds are nice. The catalog has items.
Positive lookahead (?=...) matches a position where the pattern ahead matches, without consuming characters.
Pattern breakdown:
\d+ - one or more digits(?= dollars) - only if followed by " dollars" (not captured)This matches "50" and "100" but not "30" because it's followed by "euros".
I have 50 dollars That costs 30 euros Price: 100 dollars
Negative lookbehine (?<!...) matches a position where the pattern ahead does NOT match.
Pattern breakdown:
\b\w+ - word characters(?!ing\b) - not followed by "ing" at word boundaryThis matches "test" and "walk" but not "running" or "jumping".
Source: Python Regex HOWTO - Lookahead
running test jumping walk