如何用正则表达式拆分红宝石字符串?

Sun*_*Lee 1 ruby regex

嗨,我想将此字符串拆分为以下内容。

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!"
Run Code Online (Sandbox Code Playgroud)
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!
Run Code Online (Sandbox Code Playgroud)

显然,我不能申请text.split(".")text.split(". ")。因此,首要原则是,"."除缩写的单词外,字符串将被分割。但是,我不知道如何在Ruby中做到这一点。

似乎使用Regex可能有效,但我不知道如何执行此操作。您能分享您的想法吗?

fph*_*ipe 5

基本上,您想在句点后在空格处分割,然后再输入一个大写字母:

text.split(/(?<=\.)\s+(?=[[:upper:]])/)
Run Code Online (Sandbox Code Playgroud)

正则表达式将只与空白匹配\s+,但要确保在正则表达式之前使用句号(?<=\.),后跟一个正向后缀,然后使用一个大写字母,并使用正向后缀(?=[[:upper:]])