3

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?

1 Answer 1

3

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))))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.