2015-08-08 115 views
-1

問題很簡單,我需要找到的所有值${value}例如,對於這樣的文字:

*test text $(1123) test texttest text${asd} test text test text test text ${123} test text[123132] test text [1231231]* 

我應該得到

  • asd
  • 123

我已經做了類似THIS,但你可以看到它不工作的好。

回答

2

嘗試:

\$\{([^}]+)\} 

你的地方)而不是}在字符類的否定([^}])

DEMO

+0

感謝那就是我一直在尋找的 – user3353393

1

您可以使用向後看,以獲得期望的結果:

探索更多

正則表達式(?<=\$\{)[^}]+解釋:

(?<=      look behind to see if there is: 
    \$      '$' 
    \{      '{' 
)      end of look-behind 
    [^}]+     any character except: '}' (1 or more times) 

Online Demo

示例代碼:

String str = "test text $(1123) test texttest text${asd} test text test text test text ${123} test text[123132] test text [1231231]"; 

Pattern pattern = Pattern.compile("(?<=\\$\\{)[^}]+"); 
Matcher matcher = pattern.matcher(str); 
while(matcher.find()){ 
    System.out.println(matcher.group()); 
} 

輸出:

asd 
123 
+0

我以爲你死了=) – hwnd