GeezerVison is showing github.com — add \z as a synonym for \Z in Python REs for standardization · Issue #133306 · python/cpython
add \z as a synonym for \Z in Python REs for standardization #133306Activitydavid-a-wheeler commented on May 2, 2025 +1, I'm very supportive!
Currently it's easy to make a mistake using regexes when checking inputs for security; see Correctly Using Regular Expressions. Many platforms interpret \Z as "optional newline followed by end-of-string" including Java, .NET/C#, Perl, PCRE, PHP (using PCRE), and Ruby. Many developers aren't aware that regex syntax varies between programming languages, and LLMs "borrow" regexes from other languages without necessarily fixing them. If everyone supported \A for beginning and \z for the end, we could at least have unified regular expression syntax for this common case (beginning of string .. end of string).
Thanks! collinfunk commented on May 2, 2025 sethmlarson commented on May 2, 2025 serhiy-storchaka commented on May 2, 2025 \` and \' is a terrible option.
- Since
\' usually happens at the end of the regular expression, see what it looks like: r'\`"[^"]+"\''. It is difficult to see where the end of the string. Even if you use double quotes, adjacent ' and " are hard to red: r"\`'[^']+'\'".
- On GitHub and other programmer communication sites which use backquotes to mark a code, it is difficult to use for code containing a backquote.
- There is a historical ban of using backquotes in Python syntax.
Even if \` and \' be standardized, \A is already supported in Python, and \z is supported in many other engines. \A and \z will left a preferable syntax for these anchors. Reacted by Seth Larson, wouter bolsterlee, Michiel W. Beijen and David D Lowe mstevenbrown commented on May 2, 2025 collinfunk commented on May 2, 2025
* On GitHub and other programmer communication sites which use backquotes to mark a code, it is difficult to use for code containing a backquote.
Yes, I experienced that in typing my original message. 😄
Anyways, I don't have super strong opinions. I just wanted to make sure the disagreement was known and considered. Thanks! serhiy-storchaka commented on May 3, 2025 Thank you for opening the discussion and the issue, @mstevenbrown. Thank you for additional link, @@collinfunk
\z has been added as a synonym for \Z.
Some notes:
-
AFAIK, \Z is an error inherited from PCRE 0.95 on which the first implementation of the re module was based. It was fixed in PCRE 2.0, and \z was added (https://www.pcre.org/original/changelog.txt), but it was stuck in Python for 27 years.
-
Tcl is another programming language that uses \Z for end-of-string. Perhaps it has a similar history, if was not borrowed from Python.
-
\Z may be deprecated and removed in the future, but not before all supported versions of Python support \z. It is less likely that \Z will be re-introduced with a Perl-compatible semantic (which is equivalent to (?=\n?\z)).
-
\` and \' are very inconvenient for Python.
\' followed by the closing quote is hard to read. In a non-raw string literal it should be spelled as \\' or \\\', it is easy to make mistake.
\` can be confused with \' or a grit on a screen (especially if it was "typographically enhanced", e.g. https://docutils.sourceforge.io/docs/user/smartquotes.html).
- It is difficult to use
\` on GitHub and other Markdown-based forums.
- It is difficult to use
\' in a shell script or command line.
- Currently,
\` and \' mean literal ` and '. Many users escape punctuation characters in regular expressions if they are unsure whether escaping is necessary. This is a breaking change and would require transitional period of emitting FutureWarning.
So, even if \` and \' are standardized and their support is added to Python (after a few painful years), \A and \z will remain the preferred ways to use these anchors in Python.
Reacted by Collin Funk, Mickaël Schoentgen, Miro Hrončok, Michiel W. Beijen and David D Lowe added 5 commits that reference this issue on May 3, 2025gh-133306: Use \z instead of \Z in fnmatch.translate() and glob.trans… gh-133306: Use \z instead of \Z in regular expressions in the stdlib (G… david-a-wheeler commented on May 5, 2025 Thank you VERY much, everyone! Reacted by Hugo van Kemenade, Seth Larson, Collin Funk and David D Lowe david-a-wheeler commented on May 6, 2025 Reacted by Serhiy Storchaka, Collin Funk, Hugo van Kemenade and Chris Adams
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|