1

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?

1 Answer 1

5

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:]]).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.