2012-09-28 95 views
4

這是Java中的一個字符串。如何在Java中查找字符串模式

String string="abc$[A]$def$[B]$ghi"; 

我想搜索位於$[*]$模式中的單詞。以上字符串的結果是AB

+1

[Regex](http://docs.oracle.com/javase/tutorial/essential/regex/)對你有好處嗎? – 2012-09-28 08:13:19

回答

3
String s = "abc$[A]$def$[B]$ghi"; 
    Pattern p = Pattern.compile("\\$\\[.*?\\]\\$"); 
    Matcher m = p.matcher(s); 
    while(m.find()){ 
     String b = m.group(); 
     System.out.println(">> " +b.substring(2, b.length()-2)); 
    } 
+0

謝謝你的回答。它幫助了我! – user1705636