We can use this literal to represent a compiled pattern, for example:

>>> p"(?i)[a-z]".findall("a1B2c3")
['a', 'B', 'c']

>>> compiled = p"(?<=abc)def"
>>> m = compiled.search('abcdef')
>>> m.group(0)
'def'

>>> rp'\W+'.split('Words, words, words.')
['Words', 'words', 'words', '']

This allows peephole optimizer to store compiled pattern in .pyc file, we can get performance optimization like replacing constant set by frozenset in .pyc file.

Then such issue [1] can be solved perfectly.
[1] Optimize base64.b16decode to use compiled regex
[1] https://bugs.python.org/issue35559

Two shortcomings:

1, Elevating a class in a module (re.Pattern) to language level, this sounds not very natural.
This makes Python looks like Perl.

2, We can't use regex module as a drop-in replacement: import regex as re
IMHO, I would like to see regex module be adopted into stdlib after cutting off its "full case-folding" and "fuzzy matching" features.

Related links:

[2] Chris Angelico conceived of "compiled regexes be stored in .pyc file" in March 2013.
[2] https://mail.python.org/pipermail/python-ideas/2013-March/020043.html

[3] Ken Hilton conceived of "Give regex operations more sugar" in June 2018.
[3] https://mail.python.org/pipermail/python-ideas/2018-June/051395.html

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to