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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:55:00+00:00 2026-06-14T19:55:00+00:00

I am trying to parse out all dates (possibly written in different forms) from

  • 0

I am trying to parse out all dates (possibly written in different forms) from a string. The problem is that there could be a date written in this form d/m -y for example 22/11 -12. But there could also be a date written in this form d/m with no year specified. If I find a date in this string which contains the longer form, I don’t want it to be found again in the shorter form. This is where my code fails, it finds the first date twice (one time with the year, and one time without it).

I really have two questions: (1) What is the “right” way of doing this. It really seems that I coming at this problem from the wrong angle. (2) If I should stick to this way of doing this, how comes that this line datestring.replace(match.group(0), '') don’t remove the date so I could’nt be found again?

This is my code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re

dformats = (
    '(?P<day>\d{1,2})/(?P<month>\d{1,2}) -(?P<year>\d{2})',
    '(?P<day>\d{1,2})/(?P<month>\d{1,2})',
    '(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})',
            )


def get_dates(datestring):
    """Try to extract all dates from certain strings.

    Arguments:
    - `datestring`: A string containing dates.
    """
    global dformats

    found_dates = []

    for regex in dformats:
        matches = re.finditer(regex, datestring)
        for match in matches:
            # Is supposed to make sure the same date is not found twice
            datestring.replace(match.group(0), '')

            found_dates.append(match)
    return found_dates

if __name__ == '__main__':
    dates = get_dates('1/2 -13, 5/3 & 2012-11-22')
    for date in dates:
        print date.groups()
  • 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-14T19:55:01+00:00Added an answer on June 14, 2026 at 7:55 pm

    Two ways:

    1. Use a single regular expression and use the | operator to join all your cases together:

      expr = re.compile ( r"expr1|expr2|expr3" )

    2. Find only single instances, and then pass a “start position” for the next search. Note that this will complicate things, since you want to always start with the earliest match, no matter which format is chosen. Ie, loop through all three matches, figure out which is the earliest match, do the replacement, then do it again with a incremented start position. This makes option 1 much easier regardless.

    A few additional points:

    1. Make sure you’re using “raw strings”: prepend an ‘r’ at the front of each of the strings. Otherwise the ‘\’ characters risk getting eaten up and not passed to the RE engine

    2. Consider using “sub” and a callback function in place of the “repl” parameter to do the replacement, rather than finditer. “repl” in this case is passed a match object, and should return the replacement string.

    3. Matching groups in your “single” re will have the value None if that alternative was not chosen, making it easy to detect which alternate was used.

    4. You should not say “global” unless you intend to modify that variable.

    Here’s some complete, working code.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import re
    
    expr = re.compile(
        r'(?P<day1>\d{1,2})/(?P<month1>\d{1,2}) -(?P<year>\d{2})|(?P<day2>\d{1,2})/(?P<month2>\d{1,2})|(?P<year3>\d{4})-(?P<month3>\d{2})-(?P<day3>\d{2})')
    
    
    def get_dates(datestring):
        """Try to extract all dates from certain strings.
    
        Arguments:
        - `datestring`: A string containing dates.
        """
    
        found_dates = []
        matches = expr.finditer(datestring)
        for match in matches:
            if match.group('day1'):
                found_dates.append({'day': match.group('day1'),
                                     'month': match.group('month1') })
            elif match.group('day2'):
                found_dates.append({'day': match.group('day2'),
                                    'month': match.group('month2')})
            elif match.group('day3'):
                found_dates.append({'day': match.group('day3'),
                                    'month': match.group('month3'),
                                    'year': match.group('year3')})
            else:
                raise Exception("wtf?")
        return found_dates
    
    if __name__ == '__main__':
        dates = get_dates('1/2 -13, 5/3 & 2012-11-22')
        for date in dates:
            print date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying parse out 3 pieces of information from a String . Here
I am trying to parse some dates that are coming out of a document.
Trying to parse an SQL string and pull out the parameters. Ex: select *
I'm trying to create a function to parse out all values in a multidimensional
I am trying to parse SIP messages for all SIP codes from the range
Trying to parse a csv file that has all the data wrapped in double
I am trying to parse an XML file from a URL by taking all
I am trying to parse out some information from Google's geocoding API but I
I'm trying to parse out values within a script tag with mechanize + nokogiri.
I'm trying to parse the link 'bit.ly/KATlrT' out of : asdfs asdf as dfa

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.