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?
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))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With