Given a regex like r'a (\w+) regex', I know I can capture the group, but given a captured group I want to then sub it back into the regex. I've included below a function I've built to do this, but because I'm no expert at regular expressions I'm wondering if there is a more standard implementation of such behavior, or what the "best practice" would be.
def reverse_capture(regex_string, args, kwargs):
regex_string = str(regex_string)
if not args and not kwargs :
raise ValueError("at least one of args or kwargs must be empty in reverse_capture")
if kwargs :
for kwarg in kwargs :
regex_string = re.sub(r'(?:[^\\[]|[^\\](?:\\\\)+|[^\\](?:\\\\)*\\\[)\(\?P<.+>.+(?:[^\\[]|[^\\](?:\\\\)+|[^\\](?:\\\\)*\\\[)\)',
kwarg,
regex_string)
elif args :
for arg in args :
regex_string = re.sub(r'(?:[^\\[]|[^\\](?:\\\\)+|[^\\](?:\\\\)*\\\[)\(.+(?:[^\\[]|[^\\](?:\\\\)+|[^\\](?:\\\\)*\\\[)\)',
arg,
regex_string)
else :
return regex_string
Note: the above function doesn't actually work yet, because I figured before I try covering every single case I should ask on this site.
EDIT:
I think I should clarify what I mean a bit. My goal is to write a python function such that, given a regex like r"ab(.+)c" and an argument like, "Some strinG", we can have the following:
>>> def reverse_capture(r"ab(.+)c", "Some strinG")
"abSome strinGc"
That is to say, the argument will be substituted into the regex where the capture group is. There are definitely better ways to format strings; however, the regexes are given in my use case, so this is not an option.
For any one who's curious, what I'm trying to do is create a Django package that will use a template tag to find the regex associated to some view function or named url, optionally input some of arguments, and then check if the url from the template was accessed from matches the url generated by the tag. This will solve some navigation problems. There's a simpler package which does something similar, but it doesn't serve my use case.
Examples:
If reverse_capture is the function I'm trying to write, then here are some examples of input/output (I pass in the regexes as raw strings), as well as the function call:
reverse_capture : regex string -> regex input: a regex and a string output: the regex obtained by replacing the first capture group of regex which the argument, string.
examples:
>>> reverse_capture(r'(.+)', 'TEST')
'TEST'
>>> reverse_capture(r'a longer (.+) regex', 'TEST')
'a longer TEST regex'
>>> reverse_capture(r'regex with two (.+) capture groups(.+)', 'TEST')
'regex with two TEST capture groups(.+)'
match,match.group()is the matched text.