Jackson Cunningham
Jackson Cunningham

Reputation: 5073

Rails: How to extract plain english date from string

If I have the string

string = "Robert Lee Frost (March 26, 1874 – January 29, 1963) was an American poet"

What's the best way to extract the dates from it?

Upvotes: 0

Views: 77

Answers (2)

blnc
blnc

Reputation: 4404

@tamersalama beat me to it.

((January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{2}, [0-9]{4})

Upvotes: 0

tamersalama
tamersalama

Reputation: 4133

Not sure if regexp is the best here - but you could try the following:

((January|February|March|April|May|June|July|August|September|October|November|December)\s(\d?\d),\s(\d{4}))

This should extract the whole date + the 3 components of your date (Month, Day, Year) and preserve them in the regexp variables. You can experiment with it in Rubular

str = "Robert Lee Frost (March 26, 1874 – January 29, 1963) was an American poet"
pattern = /((January|February|March|April|May|June|July|August|September|October|November|December)\s(\d?\d),\s(\d{4}))/

dates = str.scan(pattern).each do |matches|
  Date.parse(matches[0])
end

dates will hold the born/deceased dates. Be wary of no-matches.

Upvotes: 2

Related Questions