[Python-ideas] Add regex pattern literal p""
rosuav at gmail.com
Thu Dec 27 07:11:29 EST 2018
On Thu, Dec 27, 2018 at 10:49 PM Ma Lin <malincns at 163.com> wrote:
> 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)
> >>> 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.
Before discussing something specific like regex literal syntax, I
would love to see a way to measure that sort of performance
difference. Does anyone here have MacroPy experience or something and
could mock something up that would precompile and save a regex? In
theory, it would be possible to tag ANY value as "constant once
evaluated" and have it saved in the pyc. It'd be good to know just how
much benefit this precompilation actually grants.
>  Chris Angelico conceived of "compiled regexes be stored in .pyc
> file" in March 2013.
>  https://mail.python.org/pipermail/python-ideas/2013-March/020043.html
Wow that's an old post of mine :)
More information about the Python-ideas