Description
regex
adds he regex.Match.captures()
method for returning a list of all captures that a capture-group makes.
regex.Match.captures(N)
returns the list of captures for group N
just as re.Match.group(N)
return the last capture.
regex.Match.captures()
returns the list of captures for group 0
just as re.Match.group()
re
also has re.Match.groups()
that returns a tuple of all last captures.
But regex
has no way to easily return the same information. ie a tuple of lists of all captures.
Given the following example:
ip_regex = r"(([0-9]|[1-9][0-9]|1[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
ip = "127.0.0.1"
m1 = re.fullmatch(ip_regex, ip)
print(f"{m1.groups()=}")
print(f"{m1.group()=}")
print(f"{m1.group(0)=}")
print(f"{m1.group(1)=}")
print(f"{m1.group(2)=}")
print(f"{m1.group(3)=}")
m1.groups()=('0.', '0', '1')
m1.group()='127.0.0.1'
m1.group(0)='127.0.0.1'
m1.group(1)='0.'
m1.group(2)='0'
m1.group(3)='1'
m2 = regex.fullmatch(ip_regex, ip)
# nothing can go here to imitate print(f"{m1.groups()=}")
print(f"{m2.captures()=}")
print(f"{m2.captures(0)=}")
print(f"{m2.captures(1)=}")
print(f"{m2.captures(2)=}")
print(f"{m2.captures(3)=}")
# can't print (['127.', '0.', '0.'], ['127', '0', '0'], ['1'])
m2.captures()=['127.0.0.1']
m2.captures(0)=['127.0.0.1']
m2.captures(1)=['127.', '0.', '0.']
m2.captures(2)=['127', '0', '0']
m2.captures(3)=['1']
One possibility would be to rename regex.Match.captures()
to regex.Match.capture()
to match the name of re.Match.group()
Then use regex.Match.captures()
to match the name of re.Match.groups()
This would obviously be a breaking change
so alternatively adding a new method eg.
regex.Match.capturestuple()
named similar to regex.Match.capturesdict()