Returned regex groups matching string

Let's assume we have the following regular expression:

[ABC]{1,3}.{0,2}[DEFG].{2,3}V

      

We can test a Python module re

if it matches the following line:

AXDXXV

      

It fits. Then, using Python, how can we get each part of the regex matched by each part of the string?

For example, the following output list will work:

[ '[ABC]{1,3}', '.{0,2}', '[DEFG]', '.{2,3}', 'V' ]

      

+3


source to share


1 answer


You can use named capturing groups, and once you get a match, you can get the values ​​mapped to those names (c groupDict()

). I also recommend building a template like this dynamically, like OrderedDict.

See Python 2.7 Demo :



import re, collections

# Define the pattern parts with named capturing groups
parts = [('p1', r'(?P<p1>[ABC]{1,3})'),
    ('p2', r'(?P<p2>.{0,2})'),
    ('p3', r'(?P<p3>[DEFG])'),
    ('p4', r'(?P<p4>.{2,3})'),
    ('p5', r'(?P<v>V)')]
# Create and init the OrderedDict
pod = collections.OrderedDict(parts)
# Build the pattern from values (in Python 3, use list(pod.items()) )
reg = "".join([v for k,v in pod.items()])
test_str = "AXDXXV"
# Find a match
m = re.search(reg, test_str)
if m:
    # If a match is found, get the groupdict()
    m_dict = m.groupdict()
    print(m_dict)
    print("{} => {}".format(m.group("p1"), pod["p1"]))

      

The regex will look like (?P<p1>[ABC]{1,3})(?P<p2>.{0,2})(?P<p3>[DEFG])(?P<p4>.{2,3})(?P<v>V)

, and once a match is found, you get something like {'p2': 'X', 'p3': 'D', 'p1': 'A', 'p4': 'XX', 'v': 'V'}

. Then you can always check the base pattern with the value c "{} => {}".format(m.group("p1"), pod["p1"])

(for example A => (?P<p1>[ABC]{1,3})

).

+4


source







All Articles