2014-02-07 56 views
1
String text = the property value [[some.text]] and [[value2.value]]should be replaced. 

[[some.some]]應替換爲一些動態代碼。正則表達式來搜索和替換字符串中的文本

String entryValue = entry.getValue();   

      Pattern pattern = Pattern.compile("([[\\w]])"); 
      Matcher matcher = pattern.matcher(entryValue); 

      while(matcher.find()){ 

      String textToReplace = matcher.group(1); 
      textToReplace = textToReplace.replace(".",""); 

      String resolvedValue = "text to be replaced"; 
      matcher.replaceAll(resolvedValue);     
      } 

回答

1

逃生[ and ]因爲這些特殊的正則表達式符號:

Pattern pattern = Pattern.compile("(\\[\\[[\\w.]*\\]\\])"); 
+1

難道你需要兩個反斜槓? –

+0

噢,是的,糾正它。謝謝。 – anubhava

+0

你不需要轉義'''id它沒有打開並且沒有轉義'''在它之前。 – Pshemo