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 commented on Jun 11, 2023
Refs #105456
AlexWaygood commented on Jun 11, 2023
Cc. @serhiy-storchaka
raghunandanbhat commented on Jun 14, 2023
I started working on this and added parse_template() in re.__init__.py
def parse_template(source, pattern): return _parse.parse_template(source, pattern)Considering the very limited amount of documentation about this function, I would appreciate some help in testing it thoroughly and writing detailed documentation.
serhiy-storchaka commented on 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 commented on Jun 14, 2023
personally I only care about validation
raghunandanbhat commented on Jul 1, 2023
Since the parse_template() raises an error when replacement template is invalid, we could just use parse_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 commented on 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 function re._compile_template(). The type of the result is exposed as re.Template. It has minimal interface: __call__ and __class_getitem__.
asottile commented on Jun 26, 2025
ah nice! that would definitely solve my problem
asottile commented on 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 :(