user679526
user679526

Reputation: 845

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

Upvotes: 1

Views: 114

Answers (1)

anubhava
anubhava

Reputation: 785038

Escape [ and ] as these are special regex symbols:

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

Upvotes: 1

Related Questions