You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I suspect this could be as simple as exposing parse_template -- my code doesn't actually care about the return value, simply whether it raises an exception or not
Considering the very limited amount of documentation about this function, I would appreciate some help in testing it thoroughly and writing detailed documentation.
Is regex replacement template validation only what you need? Or do you need more information from the result?
Over a year ago, I wrote an implementation to compile a regex replacement template. I expected that this could be used to speed up critical code, like re.compile(), but I didn't see much difference compared to implicitly caching the compiled regex replacement templates. So I excluded it from #91524.
#135992 it is almost the same as the version written 4 years ago. It adds the compile_template() method for the pattern object which simply calls private function re._compile_template(). The type of the result is exposed as re.Template. It has minimal interface: __call__ and __class_getitem__.
unfortunate this won't make it to 3.15 (since the sre_* modules are gone gone now) -- will have to resort to some hacks until this api gets included :(
Activity
sobolevn commentedon Jun 11, 2023
Refs #105456
AlexWaygood commentedon Jun 11, 2023
Cc. @serhiy-storchaka
raghunandanbhat commentedon Jun 14, 2023
I started working on this and added
parse_template()inre.__init__.pyConsidering the very limited amount of documentation about this function, I would appreciate some help in testing it thoroughly and writing detailed documentation.
serhiy-storchaka commentedon Jun 14, 2023
Is regex replacement template validation only what you need? Or do you need more information from the result?
Over a year ago, I wrote an implementation to compile a regex replacement template. I expected that this could be used to speed up critical code, like
re.compile(), but I didn't see much difference compared to implicitly caching the compiled regex replacement templates. So I excluded it from #91524.asottile commentedon Jun 14, 2023
personally I only care about validation
raghunandanbhat commentedon Jul 1, 2023
Since the
parse_template()raises an error when replacement template is invalid, we could just useparse_template()as it is by exposing it publicly.I think it is redundant to implement a function just to check if a replacement template is valid, instead we could use the existing one as it is.
pythongh-105636: Add re.Pattern.compile_template()
pythongh-105636: Add re.Pattern.compile_template()
serhiy-storchaka commentedon Jun 26, 2025
#135992 it is almost the same as the version written 4 years ago. It adds the
compile_template()method for the pattern object which simply calls private functionre._compile_template(). The type of the result is exposed asre.Template. It has minimal interface:__call__and__class_getitem__.asottile commentedon Jun 26, 2025
ah nice! that would definitely solve my problem
asottile commentedon Jun 17, 2026
unfortunate this won't make it to 3.15 (since the sre_* modules are gone gone now) -- will have to resort to some hacks until this api gets included :(