2011-10-11 125 views
0

我正在使用Java正則表達式。 我有一個String作爲跟隨如何否定反正則表達式

String s = "the location is at {emp.address.street} with the name {emp.name},{emp.surname}"; 

對於上面的字符串,s.replaceAll("\\{(.*?)\\}", "")返回以下字符串:

the location is at with the name ,

現在我想逆此得到以下結果:

{emp.address.street}{emp.name}{emp.surname}

回答

1

這個測試腳本爲我的作品:

public class TEST 
{ 
    public static void main(String[] args) 
    { 
     String s = "the location is at {emp.address.street} with the name {emp.name},{emp.surname}"; 
     String result = s.replaceAll("[^{}]+|(\\{(.*?)\\})", "$1"); 
     System.out.println(result); 
    } 
} 
+0

這是偉大的......你能解釋一下它是如何工作的? $ 1的作用是什麼? – xbmono

0

您可以創建一個Pattern並使用Matcher.find()Matcher.group()以獲得該模式的一部分。 這裏的Javadoc文檔類:Matcher javadocPattern javadoc