Sungpah Lee
Sungpah Lee

Reputation: 1021

how to split ruby string with Regex?

Hi I want to split this string into the following.

text = "In the last summer, I visited the U.S. with my friend. It was great experience. I loved an ice cream in the U.S. Welcome to U.S.A. pal!"
In the last summer, I visited the U.S. with my friend.
It was great experience.
I loved an ice cream in the U.S.
Welcome to U.S.A. pal!

Obviously, I can't apply text.split(".") nor text.split(". "). So the thing is first rule is that the string will be split by "." with the exception of words that are abbreviated. However, I have no idea how I can do this in Ruby.

It seems that using Regex might work but I have not understood how to do this. Would you please share your idea?

Upvotes: 1

Views: 135

Answers (1)

fphilipe
fphilipe

Reputation: 10054

Basically you want to split at whitespace after a period, followed by an uppercase letter:

text.split(/(?<=\.)\s+(?=[[:upper:]])/)

The regular expression will only match the whitespace \s+, but ensure that it was preceded by a period using a positive lookbehind (?<=\.) and followed by an uppercase letter using a positive lookahead (?=[[:upper:]]).

Upvotes: 5

Related Questions