Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7837073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:27:11+00:00 2026-06-02T14:27:11+00:00

So in my Python script, I open up a text file containing dates of

  • 0

So in my Python script, I open up a text file containing dates of the format “January, 26, 1991”

Here is my regular expression:

pattern = """
(?:(September|April|June|November),\ (0?[1-9]|[12]\d|30),\ ((?:19|20)\d\d))#Months   with 30 days
|(?:(January|March|May|July|August|October|December),\ (0?[1-9]|[12]\d|3[01]),\ ((?:19|20)\d\d))#Months with 31 days
|(?:February, (?:(?:(0?[1-9]|1\d|2[0-8]),\ ((?:19|20)\d\d))|(?:(29),\ ((?:(?:19|20)(?:04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96))|2000))))#February with 28 days or 29 with a leap year
"""

r = re.compile(pattern, re.VERBOSE)

This regular expression should match any real date including February 29 on leap years.

The problem I am having is figuring out a way to go through my opened text file and put all of the matched dates into a list. I’ve tried using .match, .search, .split and the other ones but I haven’t had any luck. Is there a way to put all matches into a list as a string so that I can easily compare the list to another and find all the dates that are in both lists? Basically I would like a list to come out looking like

[“January, 1, 1990”, “February, 29, 2012”, “December, 25, 1945”,….]

Also, please let me know if the regular expression I have is correct. I modified it from the answer to another question I had and I’m not sure whether I have it right since I’m not able to see whether the dates in my text file were matched or not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:27:17+00:00Added an answer on June 2, 2026 at 2:27 pm

    You didn’t mention re.findall() in the list of things you tried. That gives you a list of all regex matches.

    However, you need to use all non-capturing groups (?:...), or you’ll get a list of all matched groups (...). Therefore, I suggest

    pattern = """
        (?:September|April|June|November)
        ,[ ] 
        (?:0?[1-9]|[12]\d|30)
        ,[ ]
        (?:19|20)\d\d # Months with 30 days
    
        |
    
        (?:January|March|May|July|August|October|December)
        ,[ ] 
        (?:0?[1-9]|[12]\d|3[01])
        ,[ ] 
        (?:19|20)\d\d # Months with 31 days
    
        |
    
        February
        ,[ ] 
        (?:
         (?:0?[1-9]|1\d|2[0-8])
         ,[ ] 
         (?:19|20)\d\d
        |
         29
         ,[ ] 
         (?:
          (?:19|20)
          (?:04|08|12|16|20|24|28|32|36|40|44|48|
             52|56|60|64|68|72|76|80|84|88|92|96)
         |
          2000
         )
        ) # February with 28 days or 29 with a leap year"""
    

    But do you really need to validate the correctness of the dates? Are you expecting false dates like February, 31, 2000 to turn up in your data? If not, you could simplify your regex enormously. Or at least delegate date validation to a date parsing function which is better equipped for this task than a monstrous regex.

    For example:

    pattern = """
        (?:January|February|March|April|May|June|
           July|August|September|October|November|December)
        ,[ ]
        [0-3]?\d
        ,[ ]
        (?:19|20)\d\d
    """
    

    matches nonsense like January, 0, 1999 or February, 31, 2000, but would it really matter?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am coding a python script that parses a text file. The format of
I'm writing a python script to replace strings from a each text file in
Say I have this simple python script: file = open('C:\\some_text.txt') print file.readlines() print file.readlines()
I am making a Python script which will read in a GeoTIFF file, and
I'm trying to make an array out of a text file My django/python code
Hi I'm having trouble posting a text file using the Python Requests Library (
I am new to PYTHON and trying to write script which replaces text in
I'm working on Windows with Python 2.6.1. I have a Unicode UTF-16 text file
If a python script uses the open(filename, r) function to open, and subsequently read,
here is an array of Unicode words used in the python script. texts =[uabc,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.