Note that there are some explanatory texts on larger screens.

plurals
  1. POMatching dates with regular expressions in Python?
    text
    copied!<p>I know that there are similar questions to mine that have been answered, but after reading through them I still don't have the solution I'm looking for.</p> <p>Using Python 3.2.2, I need to match "Month, Day, Year" with the Month being a string, Day being two digits not over 30, 31, or 28 for February and 29 for February on a leap year. (Basically a REAL and Valid date)</p> <p>This is what I have so far:</p> <pre><code>pattern = "(January|February|March|April|May|June|July|August|September|October|November|December)[,][ ](0[1-9]|[12][0-9]|3[01])[,][ ]((19|20)[0-9][0-9])" expression = re.compile(pattern) matches = expression.findall(sampleTextFile) </code></pre> <p>I'm still not too familiar with regex syntax so I may have characters in there that are unnecessary (the [,][ ] for the comma and spaces feels like the wrong way to go about it), but when I try to match "January, 26, 1991" in my sample text file, the printing out of the items in "matches" is ('January', '26', '1991', '19').</p> <p>Why does the extra '19' appear at the end?</p> <p>Also, what things could I add to or change in my regex that would allow me to validate dates properly? My plan right now is to accept nearly all dates and weed them out later using high level constructs by comparing the day grouping with the month and year grouping to see if the day should be &lt;31,30,29,28</p> <p>Any help would be much appreciated including constructive criticism on how I am going about designing my regex. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload