将文本拆分为句子,但跳过引用的内容

lul*_*ala 3 ruby regex

我想用正则表达式(使用Ruby)将一些文本拆分成句子.它不需要准确,因此可以忽略诸如"华盛顿特区"之类的情况.

但是我要求如果引用句子(通过单引号或双引号),则应该忽略它.

说我有以下文字:

一句话."哇." 爱丽丝说.塞内斯三.

它应分为三句话:

一句话.
"哇." 爱丽丝说.
句子三.

目前我有content.scan(/[^\.!\?\n]*[\.!\?\n]/),但我的报价有问题.

更新:

目前的答案可能会遇到一些性能问题.请尝试以下方法:

'Alice stood besides the table. She looked towards the rabbit, "Wait! Stop!", said Alice'.scan(regexp)
Run Code Online (Sandbox Code Playgroud)

如果有人能弄清楚如何避免它会很好.谢谢!

Tim*_*ker 8

这个怎么样:

result = subject.scan(
    /(?:      # Either match...
     "[^"]*"  # a quoted sentence
    |         # or
     [^".!?]* # anything except quotes or punctuation.
    )++       # Repeat as needed; avoid backtracking
    [.!?\s]*  # Then match optional punctuation characters and/or whitespace./x)
Run Code Online (Sandbox Code Playgroud)