2013-01-10 15 views
0

我想提取一個位於幾個方括號內的圖案。他們通常是這樣的:
this is what I want[Pat1 France�s Wal-mart ] and this is [Pat2 boy-o-boy ]

我正則表達式是這樣的:提取一些特殊字符的圖案

Pattern search = Pattern.compile("\\[[\\w.\\.\\-\\s]+]"); 

我如何可以將特殊字符或我應該在我的模式改變,提取了可能出現在這些括號的任何字符是什麼?

我想以某種方式提取[Pat1France sWal-mart]和[Pat2 boy-o-boy]。我可以提取pat2部分。感謝您的任何建議。

回答

1

你可以使用這個表達式

\\[.*?\\] 
+0

非常感謝..工作得很好。 – Knight

1

只使用其匹配任何字符.

String s= "[Pat1 France�s Wal-mart ]"; 
     Pattern p = Pattern.compile("\\[.*?\\]"); 
     Matcher m = p.matcher(s); 
     System.out.println(m.find() + " " + m.group()); 
+1

你缺少'?'否則它會貪婪地匹配 – Anirudha

+0

@ Some1.Kill.The.DJ true ..謝謝,我更關注輸出:P – PermGenError