正则表达式搜索和替换字符串中的文本

时间:2014-02-07 20:51:10

标签: java regex

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 个答案:

答案 0 :(得分:1)

转义[ and ]因为这些是特殊的正则表达式符号:

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