I don't understand why the two follow code blocks give a different result for the test case. The first code block returns 2016 as expected, but when a make a small change the second one returns None.
Here's the first one, which returns '2016' as expected
import re
date = '24 Jan 2016'
def func(line):
month_regex = re.search('(\d{1,2})\s(Jan)\s(\d{2,4})', line)
if month_regex:
year = month_regex.group(3)
return year
func(date)
Then, I add "(uary)?" to the regex and for some reason, it returns None. Note that the results of group(1) and group(2) work the same in both cases.
import re
date = '24 Jan 2016'
def func(line):
month_regex = re.search('(\d{1,2})\s(Jan(uary)?)\s(\d{2,4})', line)
if month_regex:
year = month_regex.group(3)
return year
func(date)
Why does the second code block return None?