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