I have a string (input
), within the string I want to find the following patter:
String pattern = "(src=)(\".*\")";
Then my aim is to replace the content with the new value.
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
String toReplace = "TESTTEST";
if (m.find()) {
StringBuilder res = new StringBuilder();
int start = m.start();
int end = m.end();
res = res.append(m.group(1)).append("\"").append(toReplace).append("\" ");
StringBuilder build = new StringBuilder(input);
build.replace(start, end, res.toString());
}
The problem is when I have for e.g. this text in the input string
<param src="hallo.gif" width="1024" height="768" />
and using the code above - I get following output:
<param src="TESTTEST" />
but it should be actually:
<param src="TESTTEST" width="1024" height="768" />
I have noticed already that m.end()
doesn't deliver the correct value, but I have no clue why, I tried also with a dollar symbol at the end of pattern, but then m.find()
stays false. Any idea?