Reputation: 1610
I use Python 3.8.6 with regular expression to find an English month+year combination in a string. Therefor I use the following code:
import re
regMonthPatternEn = 'an(?:uary)?|eb(?:ruary)?|march?|apr(?:il)?|may|jun(?:e)?|jul(?:y)?|aug(?:usst)?|sep(?:tember)?|oct(?:ober)?|(nov|dec)(?:ember)'
regYearPattern = '.*([1-3][0-9]{3})'
text = 'january 2018'
resultMonthEn = re.match(regMonthPatternEn,text)
resultYear = re.match(regYearPattern,text.lower())
print(text)
print(workExperiencePeriodYearList)
if (resultMonthEn and resultYear):
print('Match')
I expect that is will return "Match" in the console as output. But it returns nothing. Can you please help me to solve this problem?
Upvotes: 2
Views: 117
Reputation: 29967
From the documentation of re.match()
:
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.
Your regex does not match the j
of january
(and neither the f
for february
).
This regex will work for your test string:
regMonthPatternEn = 'jan(?:uary)|feb(?:ruary)|march?|apr(?:il)?|may|jun(?:e)?|jul(?:y)?|aug(?:usst)?|sep(?:tember)?|oct(?:ober)?|(nov|dec)(?:ember)'
Edit: as @triplee has commented, there is an extra s
in augusst
, which you might want to fix.
Upvotes: 1
Reputation: 15478
Instead use datetime
module:
import datetime
text = 'january 2018'
try:
dt = datetime.datetime.strptime(text,'%B %Y')
except ValueError:
print("Not match")
else:
print("Match")
Upvotes: 3