Issue
Wondering if there is a function such as match.groupdict()
that catches repetition similar to match.captures
function.
When I run this code:
import regex
test = regex.compile("(?P<a>a)*(?P<b>b)*(?P<c>c)*")
test.match("aabbbcc").groupdict()
I get:
{'a': 'a', 'b': 'b', 'c': 'c'}
what I would like is something like that:
{'a': ['a', 'a'], 'b': ['b', 'b', 'b'], 'c': ['c', 'c']}
Is there a function to do so or should I just do it by hand myself?
Solution
You can use
import regex
test=regex.compile("(?P<a>a)*(?P<b>b)*(?P<c>c)*")
print ( test.match("aabbbcc").capturesdict() )
# => {'a': ['a', 'a'], 'b': ['b', 'b', 'b'], 'c': ['c', 'c']}
Or, if you need to match the entire string with this pattern, replace .match
with .fullmatch
:
test.fullmatch("aabbbcc").capturesdict()
See the Python demo.
See the PyPi regex module documentation:
capturesdict
returns a dict of the named groups and lists of all the captures of those groups.
Answered By - Wiktor Stribiżew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.