简体   繁体   中英

Regex to search and replace text in a string

String text = the property value [[some.text]] and [[value2.value]]should be replaced.

The values [[some.some]] should be replaced with some dynamic code.

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);                 
            }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM