SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
Regex New Added 12 September 2026

Python: re.error: bad escape \d at position 0

The replacement argument of re.sub is not a pattern. It understands only group references such as \1 and \g<name> plus the standard string escapes, so a regex fragment or a Windows path pasted into it raises. The same text is perfectly valid as the pattern argument, which makes the message look wrong.

Quick fix

Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.

Quick fix
import re

# Raises: the replacement is not a regular expression
re.sub(r"x", r"\d", "axb")

# Escape the backslash, or pass a function when the text is arbitrary
re.sub(r"x", r"\\d", "axb")
re.sub(r"x", lambda m: r"\d", "axb")

# Windows paths are the common real world case
repl = r"C:\Users\new"
re.sub(r"PATH", repl.replace("\\", "\\\\"), template)

# Group references still work in the escaped form
re.sub(r"(\w+)@(\w+)", r"\2:\1", "ada@lovelace")

# Python 3.12 made unknown escapes in patterns an error as well, so a
# pattern that only warned on an older version can now fail outright
python3 -W error -c "import re; re.compile('\\d')"

How to diagnose Regex errors

Regex problems come in two flavours: it does not match what you expect (usually escaping, greediness, or an engine feature difference) and it matches but takes forever. The second is catastrophic backtracking, a genuine denial-of-service vector known as ReDoS, caused by nested quantifiers over overlapping character classes, such as (a+)+b. Any regex applied to untrusted input should be checked for it.

If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.

  1. Test against a visualiser that shows backtracking steps, and always test with input that fails to match. That is where catastrophic backtracking appears, not on successful matches.
  2. Eliminate nested quantifiers over overlapping classes. Rewrite (a+)+ as a+, and prefer possessive quantifiers or atomic groups where the engine supports them.
  3. Know your engine: lookbehind, named groups and Unicode property escapes differ between PCRE, RE2, JavaScript, Python and Go. RE2 (used by Go) deliberately has no backtracking and rejects some patterns outright.
  4. Anchor patterns with ^ and $ where you mean a whole-string match. Unanchored patterns silently match substrings.
  5. Set a timeout or use a linear-time engine for user-supplied patterns. Never run an untrusted regex on a request thread.

Tools worth reaching for

  • regex101.com
  • re2 / RE2J
  • Python re.DEBUG
  • grep -P for PCRE testing
  • recheck / redos linters

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

docs.python.org

Related Regex errors

See all 8 Regex errors →

Browse other categories

Something missing or wrong?

This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.