现在我正在使用.split(/(?<=[.])/)
,其中分割会保留句点。
string = "Going home. See you soon."
=> ["Going home.", "See you soon."]
但我需要的是分开并保持". "
答案 0 :(得分:2)
使用look-behind assertion,您可以在(
.
)之后匹配空格((?<=\.)
):
string = "Going home. See you soon."
string.split(/(?<=\.) /)
=> ["Going home.", "See you soon."]
答案 1 :(得分:1)
如果直截了当,为什么你需要一个正则表达式,只需提供要拆分的字符串
"Going home. See you soon.".split('. ').map{ |s| "#{s}." }