Elliot JJ
Elliot JJ

Reputation: 553

Using re with a variable list argument

I wanted to make a regex that would parse date expressions that appear periodically in a document I'm looking at, in particular, the date will sometimes be written as:

FEBRUARY 8
FEBRUARY. 8
FEBRUARY 8.
FEBRUARY    8

So my regex should look like

re.compile(MonthList+'.?.?.?.?[0-9][0-9]?')

Except that this doesn't work. How can I write a list into my regular expression such that it acts as (JANUARY|FEBRUARY|MARCH|...etc) instead of actually writing that out, or making a loop?

Upvotes: 3

Views: 124

Answers (1)

Niklas B.
Niklas B.

Reputation: 95298

You can use ordinary string manipulation to build up the regular expression. Just keep in mind that the strings in your list will be interpreted as regexes as well, unless you use re.escape to sanitize them:

r = re.compile('({}).{0,3}\d{1,2}'.format(
         '|'.join(map(re.escape, month_list))))

Upvotes: 3

Related Questions