| title | Regex Trap — Escaped Quotes in Source Code Cause Premature Non-Greedy Match Termination | |||||
|---|---|---|---|---|---|---|
| domain | development | |||||
| tags |
|
|||||
| status | published | |||||
| source | practical-experience | |||||
| confidence | 0.9 | |||||
| created | 2026-07-07 | |||||
| lang | en |
When using regex to extract default values from Python source code like os.environ.get("KEY", "default_value"), the extraction gets truncated at escaped quotes.
The standard non-greedy regex (["'])(.*?)\1 fails at \" because the regex doesn't know \" is an escape sequence — it just sees a " character.
Replace . with [^\\]|\\. to skip escaped characters:
# ❌ Breaks on escaped quotes
pattern = r'(["\'])(.*?)\1'
# ✅ Handles escaped quotes
pattern = r'(['\''"'"'])((?:[^\\\\]|\\\\.)*?)\1'import re
source = 'os.environ.get("KEY", "/@\\\\"'+:?&!=% ")'
pattern = r'(['\''"'"'])((?:[^\\\\]|\\\\.)*?)\1'
match = re.search(pattern, source)
print(match.group(2)) # /@"'+:?&!=% - This pattern works for any escape sequence:
\",\\,\',\n, etc. - For multi-line strings, add
re.DOTALLflag
Translated from Chinese lesson by zsxh1990.