Skip to content

Use raw strings for the remaining regex examples - #492

Open
prawalpokharel wants to merge 2 commits into
csev:masterfrom
prawalpokharel:raw-string-regex-examples
Open

prawalpokharel wants to merge 2 commits into
csev:masterfrom
prawalpokharel:raw-string-regex-examples

Conversation

@prawalpokharel

@prawalpokharel prawalpokharel commented Nov 18, 2026 •

Copy link
Copy Markdown

PR #447 added the r prefix to the regular expressions in code3/re05.py to re11.py. A few were missed. They still use plain strings with backslashes, so Python 3.12 and later print SyntaxWarning: invalid escape sequence for them:

  • the money example in book3/11-regex.mkd, with its copies in html3/11-regex.php and book3/translation/book-tl/11-regex.mkd
  • code3/re14.py
  • four header patterns in code3/gmane/gmane.py and two in code3/gmane/gmodel.py

The only change is the added r. Each pattern parses to the same regex as before (checked on Python 3.9 to 3.15), and re14.py prints the same output.

This fails before the change and passes after it:

python3.12 -W error -m py_compile code3/re14.py code3/gmane/gmane.py code3/gmane/gmodel.py

I left the zips alone. Running book3/make-zips.sh, or the pre-commit hook on your next commit, updates re14.py, gmane.py and gmodel.py in the code zips and 11-regex.html in book3/zips/html.zip.

Two things I noticed and left as they are:

  • In gmane.py, '\Date: ' and '\Subject: ' look like they were meant to be '\nDate: ' and '\nSubject: '. As written, \D matches any non-digit and \S any non-whitespace character, so the patterns also match headers like X-Original-Date: and X-Subject:.
  • code3/oauth.py imports cgi, which Python 3.13 removed, so the Twitter samples stop at import on 3.13 and later. Switching to urllib.parse.parse_qs fixes it.

I can send either fix as a separate PR if you want them.

PR csev#447 added the r prefix to the regular expressions in code3/re05.py
to re11.py. A few were missed and still print "SyntaxWarning: invalid
escape sequence" on Python 3.12 and later:

- the money example in book3/11-regex.mkd and its copy in
  html3/11-regex.php
- code3/re14.py
- four header patterns in code3/gmane/gmane.py and two in
  code3/gmane/gmodel.py

Only the r prefix is added. Each pattern means the same thing to the
re module as before.
@prawalpokharel prawalpokharel changed the title Use raw strings for the remaining regex examples (chapter 11, re14.py, gmane) Use raw strings for the remaining regex examples Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant