繁体   English   中英

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

[英]how to split ruby string with 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!"
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!

显然,我无法应用text.split(".")text.split(". ") 因此,首要原则是将字符串用"."分隔"." 除了缩写词。 但是,我不知道如何在Ruby中做到这一点。

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

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

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

正则表达式将只匹配空格\\s+ ,但要确保在其后加一个正向后缀(?<=\\.)的句点,再加上一个正向前瞻(?=[[:upper:]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM