简体   繁体   English

正则表达式匹配这种模式

[英]Regex to match this pattern

i have parsed some text files using stanford nlp from that output i need to extract text which matches below pattern 我已经使用stanford nlp解析了一些文本文件,我需要提取符合下面模式的文本

open bracket followed by NP or NNP or NNS etc

Input string 输入字符串

(NP (DT the) (NNP sample) (NN program))

result string 结果字符串

the ) sample ) program )

I have tried the below regex but it's not matching my required pattern 我已经尝试了下面的正则表达式,但它不匹配我所需的模式

test.replaceAll("//(//NP|//(//JJ|//(//NNS|//(//NNP|//(//NN|//(//DT|//(//CD|//(//FW|//(//WHNP|//(//WDT|//(//VBN|//(//PRP", "")

I don't know why you have all those slashes in there. 我不知道为什么你在那里有所有那些斜线。

Try this: 尝试这个:

test = test.replaceAll("\\((NP|JJ|NNS|NNP|NN|DT|CD|FW|WHNP|WDT|VBN|PRP)", "");

Here's a live demo of this code working (correctly). 以下是此代码的正常演示 (正确)。

Try "\\\\(NP|\\\\(JJ|\\\\(NNS|\\\\(NNP|\\\\(NN|\\\\(DT|\\\\(CD|\\\\(FW|\\\\(WHNP|\\\\(WDT|\\\\(VBN|\\\\(PRP" 试试"\\\\(NP|\\\\(JJ|\\\\(NNS|\\\\(NNP|\\\\(NN|\\\\(DT|\\\\(CD|\\\\(FW|\\\\(WHNP|\\\\(WDT|\\\\(VBN|\\\\(PRP"

Slashes are wrong. 斜线是错误的。 Use \\\\ instead of / 使用\\\\而不是/

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

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