2011-03-26 155 views
2

我需要在此特定句子中提取'NN'後面的單詞嗎?從文本中提取特定單詞

(ROOT (SBARQ [26.015] (WHNP [1.500] (WP [1.051] What)) (SQ[23.912] (VBZ[2.669]'s) 
(NP [19.076] (PRP$ [3.816] your) (NN [9.843] thought))) (. [0.002] ?))) 

所以,當我分析這個..使用正則表達式,我需要提取只有兩個字「想」出來。

我該怎麼做?

我的代碼:

String pattern = "\NN \[[0-9]+(?:\.[0-9])?\] (.)\)"; 
Pattern r = Pattern.compile(pattern); 
Matcher m = r.matcher(st); while(m.find()) {System.out.println(m.group());} 

output: (NN [9.843] thought))) (. [0.002] ?))) 

但我只想要'想'

答:

明白了:-)感謝的人。

String pattern = "NN \\[.*] (\\w+)"; 
Pattern r = Pattern.compile(pattern); 
Matcher m = r.matcher(st); 
while(m.find()) 
{System.out.println(m.group(1));} 

輸出:認爲

回答

0

以下正則表達式將匹配NN塊,其中所述基團將拿起 '想'(*)。

\(NN \[[0-9]+(?:\.[0-9]*)?\] (.*)\) 

我總是發現正則表達式測試牀對於這類問題非常有用。我建議使用: http://www.gskinner.com/RegExr/

+0

我試了兩個..似乎沒有工作。 – madCode 2011-03-27 08:50:39

+0

嘿,感謝您的鏈接,它很棒。 – madCode 2011-03-27 09:29:26

2

鑑於格式不允許多淫東西,這應該得到了這個詞:

\(NN \[[^\]]*\] ([^\)]*)\) 

,然後做某事物像

if (matcher.find(yourstring)) { 
    theword = matcher.group(1); 
} 
+0

似乎已經吞噬了大括號。試試這個:'\(NN \ [[^ \]] * \]([^ \)] *)\)' – Nodebody 2011-03-27 10:00:28

相關問題