2016-10-31 61 views
0

我想多个报价匹配单个句子的内部,例如行:匹配多个报价在句子

Hello "this" is a "test" example.

这是我使用的正则表达式,但我有一些与它的问题:

/[^\.\?\!\'\"]{1,}[\"\'\“][^\"\'\“\”]{1,}[\"\'\“\”][^\.\?\!]{1,}[\.\?\!]/g

我试图用这个正则表达式实现的是发现一切从最后一句,直到我打了引号的开始,然后找到收盘集合并持续到无论是.?!

,我使用来测试示例文本是从恶魔的呼唤:

什么似乎是主文档的标题是在精心印制,以避免错误的字符“邪神崇拜”读一个如此前所未闻的词。手稿分为两部分,第一部分是“1925年 - 罗纳德普罗维登斯托马斯街7号,威尔考克斯的梦想和梦想工作”,以及第二部分,“检查员约翰·雷格拉斯的叙述,121比维尔St.,New Orleans,La。,at 1908年AAS Mtg.-Notes on Same,&韦伯教授的论文“其他稿件都是简要说明,其中一些论述了不同人的奇怪梦想,其中一些来自哲学书籍和杂志的引文。

问题出现在线The manuscript was...。有谁知道如何解释这样的重复?或者,还有更好的方法?

+0

https://regex101.com/r/7oFInG/1没有工作。 – user123

+0

您可能已经知道这一点,但以防万一,[Regexr](http://regexr.com/)是一个很好的工具来找出这样的东西。 –

+0

是的,这是我一直在使用的,我可以做类似于我上面的东西,但寻找另一组引号,但后来我会得到一个非常静态的正则表达式。 – user123

回答

0

这一个忽略引号内的[。?!]。但在这种情况下,像Acct.” The nth这样的案例将被视为单个句子。在那里可能没有.

var r = 'What seemed to be the main document was headed “CTHULHU.?! CULT” in characters painstakingly printed to avoid the erroneous reading of a word so unheard-of. The manuscript was divided into two sections, the first of which was headed “1925—Dream and Dream Work of H. A. Wilcox, 7 Thomas St., Providence, R.I.”, and the second, “Narrative of Inspector John R. Legrasse, 121 Bienville St., New Orleans, La., at 1908 A. A. S. Mtg.—Notes on Same, & Prof. Webb’s Acct.” The other manuscript papers were all brief notes, some of them accounts of the queer dreams of different persons, some of them citations from theosophical books and magazines.' 
 
.split(/[“”]/g) 
 
.map((x,i)=>(i%2)?x.replace(/[.?!]/g,''):x) 
 
.join("'") 
 
.split(/[.?!]/g) 
 
.filter(x => x.trim()).map(x => ({ 
 
    sentence: x, 
 
    quotescount: x.split("'").length - 1 
 
})); 
 

 
console.log(r);

+0

这与我正在寻找的内容很接近,但是我想忽略引号内的标点符号,并且只在引号之外进行拆分。 看看我发布在OP中的正则表达式,它应该在运行时显示问题。 – user123

+0

答案已更新,但是“Acct”。“将被视为单个句子。此外,引号内的标点符号也被删除,如果需要可以恢复。 – sabithpocker

0

你可以使用这个天真的模式:

/[^"'“.!?]*(?:"[^"*]"[^"'“.!?]*|'[^']*'[^"'“.!?]*|“[^”]*”[^"'“.!?]*)*[.!?]/ 

细节:

/ 
[^"'“.!?]*   # all that isn't a quote or a punct that ends the sentence 
(?: 
    "[^"*]" [^"'“.!?]* 
    | 
    '[^']*' [^"'“.!?]* 
    | 
    “[^”]*” [^"'“.!?]* 
)* 
[.!?] 
/

如果你想要的东西越强,你可以模拟“原子团“功能,特别是如果你不确定每个openi纳克报价具有闭引号(以防止灾难性回溯):

/(?=([^"'“.!?]*))\1(?:"(?=([^"*]))\2"[^"'“.!?]*|'(?=([^']*))\3'[^"'“.!?]*|“(?=([^”]*))\4”[^"'“.!?]*)*[.!?]/ 

的原子团不允许对回溯关闭一次。不幸的是,这个功能在Javascript中不存在。但是,有一种方法使用一个超前那自然是原子,捕获组和反向引用效仿:

(?>expr) => (?=(expr))\1