2017-05-13 26 views
0

假設我們有以下內容其中有三行內容:這裏查找第一個開括號正則表達式的記事本+

cnt1 {\abc asd{} sd {{d}} def.} 
cnt2 {\abc hgj{} sd {sd} {{d}} def.} 
cnt3 {\def asd{} sd {{d}} def.} 

我們可以看到,許多打開和關閉括號,我可以同時過濾掉內容在第一行和第二行中,它具有類似的內容「{\ abc」和每行的開始,但每個內容中都有不同數量的大括號和內容。我只想到這個問題是如何檢測其起始內容是「\ abc」的每一行的第一個大括號(父括號)的內容。假設內容「{\ abc asd {sd {{d}} def。}」= $ 1,並且「{\ abc hgj {} sd {sd} {{d}} def。}」=「港幣$ 16。我怎樣才能讓理想的結果作爲

cnt1 x$1y 
cnt2 g$2h 
cnt3 {\def asd{} sd {{d}} def.} 

(x,y和G,H被添加到給定內容的雙方) 太謝謝你了!

+0

請看看我的問題... – greenworld

回答

0

您可以使用簡單的regex這樣的:{\\abc.*}

String str = 
      "cnt1 {\\abc asd{} sd {{d}} def.}\n" + 
      "cnt2 {\\abc hgj{} sd {sd} {{d}} def.}\n" + 

      "cnt3 {\\def asd{} sd {{d}} def.}\n"; 
    Pattern pat=Pattern.compile("\\{\\\\abc.*}"); 
    Matcher matcher=pat.matcher(str); 
    while(matcher.find()){ 
     System.out.println(matcher.group(0)); 
    } 

顯然{不得不逃出

輸出:

{\abc asd{} sd {{d}} def.} 
{\abc hgj{} sd {sd} {{d}} def.} 
+0

我想在Notepad ++中做到這一點 – greenworld

相關問題