OJFord
OJFord

Reputation: 11130

re.match one from a set of strings, with result as variable

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

Answers (3)

Jon Clements
Jon Clements

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

vks
vks

Reputation: 67968

 (^test$|^test1$|^test2$)

You can use this.basically add more precision and get the desired result.

Upvotes: 0

unutbu
unutbu

Reputation: 879919

First match wins, so order matters:

In [2]: re.match(r'(test2|test3|test)','test2').group(1)
Out[2]: 'test2'

Upvotes: 3

Related Questions