For this project, you will be writing regular expressions that look for and find all instances of a date in any given string.  Your program should: Find the following different formats for dates: August 2nd, 1994 august 2, 1994  08/02/1994 08/02/94 08-02-1994 Your program should be case insensitive using re.IGNORECASE Your program can only have a maximum of two different regexes. A special prize goes to those of you who can fit all of this into one regex Your program should be able to tell if the date is a valid date. For example, if the date says 99/99/99, then your program should ignore this as a date. HINT: this is not done within the regex. Try saving your dates to something like a list and going through that list after your regex runs.  At the end of your program, it should print out all of the dates in the following format: mm/dd/yyyy. EX. 08/02/1994 Your program should use at least two character classes, you may need more. Feel free to make your own custom classes as well.  The following special characters are required in some capacity: {}, ?, If you're shooting for one regex to get the special prize, then you'll need to use lots of groups. Depending on how you build your program, you may need to use several groups regardless.  Make sure to use re.verbose() so you don't have a super ugly regex.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question
100%

For this project, you will be writing regular expressions that look for and find all instances of a date in any given string. 

Your program should:

  • Find the following different formats for dates:
    • August 2nd, 1994
    • august 2, 1994 
    • 08/02/1994
    • 08/02/94
    • 08-02-1994
  • Your program should be case insensitive using re.IGNORECASE
  • Your program can only have a maximum of two different regexes. A special prize goes to those of you who can fit all of this into one regex
  • Your program should be able to tell if the date is a valid date. For example, if the date says 99/99/99, then your program should ignore this as a date. HINT: this is not done within the regex. Try saving your dates to something like a list and going through that list after your regex runs. 
  • At the end of your program, it should print out all of the dates in the following format: mm/dd/yyyy. EX. 08/02/1994
  • Your program should use at least two character classes, you may need more. Feel free to make your own custom classes as well. 
  • The following special characters are required in some capacity: {}, ?,
  • If you're shooting for one regex to get the special prize, then you'll need to use lots of groups. Depending on how you build your program, you may need to use several groups regardless. 
  • Make sure to use re.verbose() so you don't have a super ugly regex. 
Expert Solution
Step 1: Here's a regular expression that should be able to find all instances of a date in a string

Sure, I can help you with that. Here's a regular expression that should be able to find all instances of a date in any given string, and check if the date is a valid one:

CODE in Python:

import re

date_regex = r'''
    (?P<month>
        January|February|March|April|May|June|
        July|August|September|October|November|December|
        january|february|march|april|may|june|
        july|august|september|october|november|december|
        jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec
    ) # month
    [\s-]?
    (?P<day>\d{1,2}) # day
    (st|nd|rd|th)?[\s,]?
    [\s-]?
    (?P<year>\d{2}|\d{4}) # year
    '''

dates = []
for match in re.finditer(date_regex, text, re.IGNORECASE | re.VERBOSE):
    month = match.group('month').lower()
    day = match.group('day')
    year = match.group('year')

    if len(year) == 2:
        year = '19' + year

    try:
        date_obj = datetime.datetime.strptime(f'{month} {day} {year}', '%B %d %Y')
        dates.append(date_obj.strftime('%m/%d/%Y'))
    except ValueError:
        pass

print(dates)

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT