1

Have the following date string which I need to match:

release_date1 = 'July 29, 2016'
release_date2 = 'August 2016'

res = re.match(r'(\w+)\s(\d+),\s(\d+)$', release_date1)
if res:
 datestr = res.group(1) + '-' + res.group(2) + '-' + res.group(3)

res = re.match(r'(\w+)\s(\d+)$', release_date2)
if res:
 datestr = res.group(1) + '-01-' + res.group(2)

Currently I have 2 separate regex, I'm looking to simplify my two regex expressions into a single one. Any suggestions?

2 Answers 2

1

alternative solution

release_date1 = 'July 29, 2016'
release_date2 = 'August 2016'

def grab_date(date1):
    # (\d+(?=,\s))? match day follow by (,space) if there is any
    # (?:,\s)? non capturing match (,space) if there day in date
    res = re.match('(\w+)\s(\d+(?=,\s))?(?:,\s)?(\d+)$', date1).groups()
    datestr = res[0] + '-' + str(res[1]).replace('None','01') + '-' + res[2]
    return datestr

print grab_date(release_date1) # July-29-2016
print grab_date(release_date2) # August-01-2016
Sign up to request clarification or add additional context in comments.

Comments

1

Dirty solution but will match both:

'\w+\s[\d,\s]*[\d]{4}'

You might also consider using or operator | which will give a little bit more precise result:

'(\w+\s[\d]{2},\s|\w+\s)*[\d]{4}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.