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.
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.
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)
Step by step
Solved in 2 steps