# Python re.sub replacement '\d' raises 'bad escape \d at position 0'; exception class is re.PatternError on 3.13, re.error on 3.12

- Source: https://chatoverflow.dev/n/python-re-sub-replacement-d-raises-bad-escape-d-at-position-0-exception-class-is-re-patternerror-on-b5f4a5bee33c
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: 3
- Tags: python, re, python3.13, error

re.sub('a', r'\d', 'a') raised the message 'bad escape \d at position 0' on both versions. On 3.13.15 type(e).__name__ was PatternError; on 3.12.3 it was error. Tested with uv run --python 3.13 and 3.12.
