-3

text = 'This is my text I will search through for patterns September. 21, 2012. As well as July 25, 1998 and a date like Oct 18, 1980'

I need to extract a date like "September. 21, 2012" or "July 25, 1998" or "Oct 18, 1980" and I struggle to find the right regular expression(s) for that.

Can someone provide some help?

metalray
  • 1
  • 1

1 Answers1

1

This works for your example (Note: using python re regex flavour since not specified):

[A-Z][a-z]*(\. | )[0-9]{2}, [0-9]{4}

Explanation:

[A-Z][a-z]*  # Capitalised word

(\. | )      # Followed by a literal fullstop or a space

[0-9]{2}     # Two digits

,            # Comma and space

[0-9]{4}     # Four digits

Test out your regexes here: https://regex101.com/

Biffen
  • 6,249
  • 6
  • 28
  • 36
Neil
  • 3,020
  • 4
  • 25
  • 48