[英]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.