In: preferences = 'Hello my name is paul. I hate puzzles.'
I want to extract Hello my name is paul.
In: preferences = 'Salutations my name is richard. I love pizza. I hate rain.'
I want to extract Salutations my name is richard. I love pizza.
In: preferences = 'Hi my name is bob. I enjoy ice cream.'
I want to extract Hi my name is bob. I enjoy ice cream.
In other words, I would like to
- always discard
preferences = ' - discard any last sentence (delimited by
.) that has the wordhatein, if present. - discard the final
'
My problem is that my regex stops at the first . and doesn’t extract the subsequent sentences.
Thanks.
You can achieve what you want using a regular expression:
That one isn’t too tricky:
(.*?\.)– Match your expected output, that will be captured in group$1. The pattern matches “sentences” (as you’ve defined), but lazily (*?), as few as it must.(?:[^.]*\bhate\b[^.]*\.)?– optionally match the last sentence, but only if it contains “hate”. If it can match, and it is the last sentence, the matching engine will not backtrack, and the last sentence will not be included in the captured group.Here’s a working example in Rubular: http://www.rubular.com/r/qTuMmB3ySj
(I’ve added
\r\nin a few places, to avoid[^.]matching new lines)Honestly though, you can do better than a single regular expression here, if you can avoid it.