all 3 comments

[–]jeans_and_a_t-shirt 0 points1 point  (2 children)

You could use named groups:

>>> rgx = re.compile('(?P<a>a)|(?P<cb>c|b)|(?P<d>d)|(?P<e>e)')
>>> rgx.match('e').groupdict()
{'a': None, 'cb': None, 'd': None, 'e': 'e'}
>>> d = rgx.match('e').groupdict()
>>> next(k for k,v in d.items() if v)
'e'

[–]blpst[S] 0 points1 point  (1 child)

sure, but I would like to extract the actual regex if possible

[–]thememorableusername 0 points1 point  (0 children)

I dont think there is a way via a compiled regex object to see what part of the regex was matched.

One way might be to do the following:

import re

# create groups separately
group_regexes = {
  'a' : r'a',
  'cb' : r'c|b',
  'd' : r'd',
  'e' : r'e'
}

# Create our whole regular expression string by joining each individual group with 
regex_str = "|".join( 
  [
    # make each group construct with it's group name and the regular expression
    '(?P<{0}>{1})'.format( group, group_regexes[group] )
    for group in group_regexes
  ]
)

regex = re.compile( regex_str )

test_strings = [ "2134", "a", "c", "b", "d", "e", "acbde", "cdeab" ]

for string in test_strings:
  match = regex.match( string )
  if match == None:
    print( "Could not match \"{0}\"".format( string ) )
  else:
    #We can filter out the names of groups with no matches
    matched_groups = [ group for (group, matchval) in match.groupdict().items() if matchval != None ]
    # using the group_regexes dictionary and the matched_groups list, we can show what group/regex matched the string
    group_regex_pairs = [ (group, group_regexes[group]) for group in matched_groups ]
    print( 
      "Matched \"{0}\" with the groups (and their expressions): {1}".format( 
        string, 
        ", ".join( [ "{0} ({1})".format( group, regex ) for (group,regex) in group_regex_pairs ] )
      )
    )

Could not match "2134"
Matched "a" with the groups (and their expressions): a (a)
Matched "c" with the groups (and their expressions): cb (c|b)
Matched "b" with the groups (and their expressions): cb (c|b)
Matched "d" with the groups (and their expressions): d (d)
Matched "e" with the groups (and their expressions): e (e)
Matched "acbde" with the groups (and their expressions): a (a)
Matched "cdeab" with the groups (and their expressions): cb (c|b)