简体   繁体   English

如何使用多个定界符在ruby'split'中定界?

[英]how can I delimit in ruby 'split' with multiple delimiters?

I have the following code. 我有以下代码。 My intend is to be able to parse the last full sentence in a string of sentences: 我的意图是能够解析句子字符串中的最后一个完整句子:

string = "something for nothing.  'nothing for free.'"
string.split(/!|\.|\.'|\?/)
=> ["something for nothing", "  'nothing for free", "'"]

I would like to be able to do array.last and get the last sentence, whether it ends in '!', '?', '.', or the end of a quote like, ." or .' 我希望能够执行array.last并获取最后一个句子,无论其结尾是'!','?','。'还是引号(如“。”或。)的结尾。

But when I try to include a combination, as above, it doesn't treat the .' 但是,当我尝试包括上述组合时,它不会处理.' part as a single delimiter. 部分作为单个定界符。

As I can see, there are two spaces between the sentences. 如我所见,句子之间有两个空格。 So just split on them, instead of using a regex, which is not needed here at all. 因此,只需拆分它们,而不使用正则表达式,这里根本不需要。

puts string.split("  ").last #=> 'nothing for free.'

If punctuation is guaranteed, then you can use the rex 如果可以保证标点符号,则可以使用rex

puts string.split(/(?<=[.?!]("|'|))\s+/).last

The regex /(?<=[.?!]("|'|\\s))\\s+/ uses lookbehind and splits on the space after . or ? or ! + " or ' or space. 正则表达式/(?<=[.?!]("|'|\\s))\\s+/使用向后搜索,并在.?! + "'或空格之后的空格上进行拆分。

string = "something for nothing.  'nothing for free.'. Something for free? '...Everything for FREE!!!!!!...' "

string.split(/\b?\.\s|\?\s|\!\s/)

=> ["something for nothing", " 'nothing for free.'", "Something for free", "'...Everything for FREE!!!!!!...' "] => [“一无所有”,“一无所有。”,“一无所有”,“'...一切都是免费的!!!!!! ...'“]

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

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