I need a regular expression that matches sentences that end with one of three words.
For example assume the three words are: miles and yards.
My_String = One October 5, 2012 a race was run in Washington, D.C. There were 400 runners in the race. The distance of the race was ten miles.
Given the string I would like to match = The distance of the race was ten miles.
I have tried the following: regex = /.\s.*[miles.|yards.]/
and match = match.regex(My_String)
`
But this is matching all the sentence before the sentence I’m interested as well as the sentence I’m interested in.
Update:
Kirill Polishchuk has come up with a solution that seems to get me pretty close (see below). Unfortunately, when I put this into the Rubular expression tester it simply highlights the target sentence but does not return it as a match.
I’m starting to think that Kirill’s expression is actually working but it may not be supported in the Rubular engine I’m using to test it.
Final Update:
The following expression successfully matched the target sentence:
([^.]*(?:miles|yards)\.)
Thanks for everyone’s help.
Try this:
In this regex I assume that sentence ends with
.symbol. It is really complicated to determine bounds of sentence using regex.