簡體   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