Since as you claim this is not valid XML document you can try with regex. To replace founded value with its new version you can use appendReplacement
and appendTail
from Matcher
class.
appendReplacement
replace founded value with its new version. You decide how you want to replace it.appendTail
adds part after last match to buffer.
To find match between <change>
and </change>
you can use <change>(.*?)</change>
regex - if you want dot to represent all characters (including line separators like \n
) you should use DOTALL
flag from Pattern
.
Demo:
String input = "random text blah blah <change>hello</change> more random text <change>hey</change> ..";
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("<change>(.*?)</change>",Pattern.DOTALL);
Matcher m = p.matcher(input);
while(m.find()){
String valueFromTags = m.group(1);
m.appendReplacement(sb, valueFromTags.toUpperCase());
// ^^^^^^^^^^^^^
// you decide what to put as replacement of original value
// toUpperCase is just example
}
m.appendTail(sb);
String result = sb.toString();
System.out.println(result);
Output:
random text blah blah HELLO more random text HEY ..