Search code examples
pythonpattern-matchingstring-parsing

re.findall only finding half the patterns


I'm using re.findall to parse the year and month from a string, however it is only outputting patterns from half the string. Why is this?

date_string = '2011-1-1_2012-1-3,2015-3-1_2015-3-3'

find_year_and_month = re.findall('[1-2][0-9][0-9][0-9]-[1-12]', date_string)

print(find_year_and_month)

and my output is this:

['2011-1', '2012-1']

This is the current output for those dates but why am I only getting pattern matching for half the string?


Solution

  • [1-12] doesn't do what you think it does. It matches anything in the range 1 to 1, or it matches a 2.

    See this question for some replacement regex options, like ([1-9]|1[0-2]): How to represent regex number ranges (e.g. 1 to 12)?

    If you want an interactive tool for experimenting with regexes, I personally recommend Regexr.