2016-01-23 93 views
3

我有以下文字:正則表達式匹配的文本

node [ 
    id 2 
    label "node 2" 
    thisIsASampleAttribute 43 
] 
node [ 
    id 3 
    label "node 3" 
    thisIsASampleAttribute 44 
] 

我想組的每個節點和它的括號內如內容:

node [ 
    id 2 
    label "node 2" 
    thisIsASampleAttribute 43 
] 

不過我我下面的代碼分組的全文:

Pattern p = Pattern.compile("node \\[\n(.*|\n)*?\\]", Pattern.MULTILINE); 

Matcher m = p.matcher(text); 

while(m.find()) 
{ 
    System.out.println(m.group()); 
} 

編輯文本:

node [\n" + 
"  id 2\n" + 
"  label \"node 2\"\n" + 
"  thisIsASampleAttribute 43\n" + 
" ]\n" + 
" node [\n" + 
"  id 3\n" + 
"  label \"node 3\"\n" + 
"  thisIsASampleAttribute 44\n" + 
" ]\n" 
+0

你有足夠的斜線? – 2016-01-23 01:11:19

回答

2

問題是你只捕獲最後一個字符(.*|\n)*?(因爲.?不在捕獲組內)。

您可以將捕獲組更改爲非捕獲組,然後用捕獲組包裝該捕獲組並將其與*?包裝在一起以捕獲所有匹配((?:.*?|\n)*?)

Example Here

Pattern p = Pattern.compile("node \\[\\n((?:.*?|\\n)*?)\\]", Pattern.MULTILINE); 
Matcher m = p.matcher(text); 
while(m.find()) 
{ 
    System.out.println(m.group(1)); 
} 

但是,正則表達式以上是相對低效的。一種可能更好的方法是將非]字符與否定字符集([^\]]*)匹配。

Example Here

Pattern p = Pattern.compile("node \\[\\n([^\\]]*)\\]", Pattern.MULTILINE); 
Matcher m = p.matcher(text); 
while(m.find()) 
{ 
    System.out.println(m.group(1)); 
} 
+0

我不是Java專家,但爲什麼它只需要'\ n'中的一個斜槓和'\\ [''中的兩個斜槓? – 2016-01-23 01:19:55

+0

似乎仍然是分組的一切。如果有幫助,我已經更新了包含字符的文本的問題 – joe

+0

@joe我添加了示例..您是否在檢索第一個捕獲組? 'm.group(1)'? –