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 7793049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:21:50+00:00 2026-06-01T22:21:50+00:00

So essentially I am trying to read lines from multiple files in a directory

  • 0

So essentially I am trying to read lines from multiple files in a directory and using a regex to specifically find the beginnings of a sort of time stamp, I want to also place an instance of a list of months within the regex and then create a counter for each month based on how many times it appears. I have some code below, but it is still a work in progress. I know I closed off date_parse, but I that’s why I’m asking. And please leave another suggestion if you can think of a more efficient method. thanks.

months = ['Jan','Feb','Mar','Apr','May','Jun',\
          'Jul','Aug','Sep','Oct','Nov','  Dec']
date_parse = re.compile('[Date:\s]+[[A-Za-z]{3},]+[[0-9]{1,2}\s]')
counter=0
for line in sys.stdin:
    if data_parse.match(line):
        for month in months in line:
            print '%s %d' % (month, counter)
  • 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-01T22:21:51+00:00Added an answer on June 1, 2026 at 10:21 pm

    In a regular expression, you can have a list of alternative patterns, separated using vertical bars.

    http://docs.python.org/library/re.html

    from collections import defaultdict
    
    date_parse = re.compile(r'Date:\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)')
    
    c = defaultdict(int)
    
    for line in sys.stdin:
        m = date_parse.match(line)
        if m is None:
            # pattern did not match
            # could handle error or log it here if desired
            continue # skip to handling next input line
        month = m.group(1)
        c[month] += 1
    

    Some notes:

    • I recommend you use a raw string (with r'' or r"") for a pattern, so that backslashes will not become string escapes. For example, inside a normal string, \s is not an escape and you will get a backslash followed by an ‘s’, but \n is an escape and you will get a single character (a newline).

    • In a regular expression, when you enclose a series of characters in square brackets, you get a “character class” that matches any of the characters. So when you put [Date:\s]+ you would match Date: but you would also match taD:e or any other combination of those characters. It’s perfectly okay to just put in a string that should match itself, like Date:.

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

Sidebar

Related Questions

I'm trying to read a text document that is separated into essentially paragraphs of
Essentially, I'm trying to store user input in a multi dimensional array using for
I'm trying to get cdata text from a node using dom4j java. My issue
I've read up a good few articles etc., here and from Google, about trying
I'm trying to read some large text files (between 50M-200M), doing simple text replacement
I'm trying to paint essentially a checkerboard pattern to the screen by using a
So What I'm essentially trying to do is have something happen 70% of the
I'm essentially trying to set it up so that when I click a button
I'm looking for suggestions for a beginner level ASP/XML test. Essentially I'm trying to
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a

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.