Faster, Faster! Improving regex performance with atomic grouping, possessive quantifiers and more!
summary
Benchmarks run on Python 3.11 (CPython 3.11.4) on Windows 10 and timed with an IPython timer show concrete fixes: compile patterns (module cache actually defaults to 512 patterns), avoid dot and nested unbounded quantifiers, use negated classes, atomic grouping and possessive quantifiers to eliminate catastrophic backtracking. Reported wins include ~500% for startswith vs regex, 280% from clamping quantifiers, 78% by removing overlapping classes, 26% for atomic groups and even a cited 2 million percent case when possessive quantifiers remove exponential work.
AI-written summary · about
chapters (23)
- 0:00Regex performance intro
- 2:08Rule one: avoid regex
- 3:10Avoid regex for simple matches
- 4:37Pre-compile regex patterns
- 5:41Replace flags with classes
- 6:29Quantifiers and failed cases
- 7:31Avoid plus and star quantifiers
- 8:15Greedy vs lazy quantifiers
- 9:21Negated classes vs dot
- 10:49Non-capturing groups
- 11:31Regex backtracking
- 13:21Catastrophic backtracking
- 15:30Solutions to backtracking
- 16:34Mutually exclusive classes
- 16:56Atomic grouping and possessive quantifiers
- 18:43When possessive quantifiers work
- 19:49Possessive in quoted strings
- 20:12Speed boost with possessive
- 21:57Key regex tips
- 22:40Avoid nested unbounded quantifiers
- 23:22Python 3.11 regex updates
- 24:47Regex engine differences
- 26:16Q&A on possessive quantifiers
related talks
30mWrite faster Python! Common performance anti patterns
34mDynamic Pattern Matching with Python
30mAdvanced parsing of structured data using Python's new match statement
34mAn Intuitive Grasp of RegEx’s in Python
19mPython - best practices
42mIntuition vs. Reality: Surprising Truths in Python Performance
10mAn Exploration of Python's Upcoming Match Statement
01h 12mFurious & Fast Python 7: Writing Fast Python Code
