Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using re with a variable list argument

Tags:

python

regex

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?

like image 237
Elliot JJ Avatar asked Feb 03 '26 08:02

Elliot JJ


1 Answers

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))))
like image 154
Niklas B. Avatar answered Feb 05 '26 20:02

Niklas B.