0

複数のオプションの正規表現を任意の順序で文字列に一致させることは可能ですか (ただし、特定の順序で取得できますか?)

例えば、

s = '(パターン1)(パターン2)(パターン3)'

そのような

match = re.search(s, 'pattern2 pattern1')
match = re.search(s, 'pattern1 pattern3 pattern2')
match = re.search(s, 'pattern3 pattern1')

および他のすべての順列一致、さらに

match.groups()

pattern1pattern2pattern3を常に同じ順序で返します。None

私はこれがありそうにないように聞こえることを知っています.

4

2 に答える 2

1

ということですか

s = '(pattern1|pattern2|pattern3)'
match = sorted(re.findall(s, 'pattern1 pattern3 pattern2'))
match
>>> ['pattern1', 'pattern2', 'pattern3']

?

于 2013-02-18T21:58:47.097 に答える
0

step one read the documentation for itertools and see what itertools magic will generate the kind of sets of matches you want. For example

>>> import itertools
>>> a=['aaa','bbb','ccc']
>>> for q in itertools.permutations(a):
...   print q
... 
('aaa', 'bbb', 'ccc')
('aaa', 'ccc', 'bbb')
('bbb', 'aaa', 'ccc')
('bbb', 'ccc', 'aaa')
('ccc', 'aaa', 'bbb')
('ccc', 'bbb', 'aaa')

To ensure that the matches are returned in a consistent manner tag each part of the regexp with ?P for example

>>> rl=[]
>>> bigr=""
>>> for q in itertools.permutations(a):
...   r=""
...   for ms in q:
...     r = r + "(?P<" + ms + ">" + ms + ")"
...   rl.append(r)
... 
>>> rl
['(?P<aaa>aaa)(?P<bbb>bbb)(?P<ccc>ccc)', '(?P<aaa>aaa)(?P<ccc>ccc)(?P<bbb>bbb)', '(?P<bbb>bbb)(?P<aaa>aaa)(?P<ccc>ccc)', '(?P<bbb>bbb)(?P<ccc>ccc)(?P<aaa>aaa)', '(?P<ccc>ccc)(?P<aaa>aaa)(?P<bbb>bbb)', '(?P<ccc>ccc)(?P<bbb>bbb)(?P<aaa>aaa)']

In the example above I've used the match strings as the id tags in the P part of the expression. You could generate a "name1" "name2" or similar instead

Finally, join up all the little regexps into one giant regexp

onegiantregexp = "|".join(rl)

And use something like the re module "groupdict" to get the results

Hope this helps

于 2013-02-18T22:09:10.337 に答える