Reputation: 11130
I would like to match a pattern where there may be one of a few strings of characters, and I want to know which of those was matched.
So I tried:
>>> re.match(r'(test|test2|test3)','test2').group(1)
'test'
But I expected the result to be test2
.
How can I do this, without permitting any string and testing which it was separately?
Upvotes: 2
Views: 41
Reputation: 142176
You can generalise this to a helper function that automatically prepares a regex by putting longest matches first and returns a compiled regex ready for use, eg:
import re
def match_one_of(*options):
options = [re.escape(option) for option in options]
options.sort(key=len, reverse=True)
return re.compile('|'.join(options))
rx = match_one_of('test', 'test2', 'test3')
print rx.match('test2').group()
# test2
If you already a list of options, then:
options = ['test', 'test2', 'test3']
rx = match_one_of(*options)
Upvotes: 1
Reputation: 67968
(^test$|^test1$|^test2$)
You can use this.basically add more precision and get the desired result.
Upvotes: 0
Reputation: 879919
First match wins, so order matters:
In [2]: re.match(r'(test2|test3|test)','test2').group(1)
Out[2]: 'test2'
Upvotes: 3