I have a pattern and a string as an input, I'm looking to find that pattern N times in the input but if doesn't work.
I tried with following pattern:
private static Pattern test = Pattern.compile("(<tag>)(.*)(</tag>)");
private static String input = "<tag>hello</tag>blablabla<tag>world</tag>";
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.change(input));
}
public static String change(String text) {
StringBuilder s = new StringBuilder();
try {
Matcher m = test.matcher(text);
int pos = 0;
while (m.find()) {
s.append(text, pos, m.start());
pos = m.end();
s.append("&&" + m.group(2) + "&&");
}
s.append(text, pos, text.length());
} catch (Exception e) {
System.out.println("Exception " + e);
}
return s.toString();
}
this is what I'm getting:
&&hello</tag>blablabla<tag>world&&
and this is the result what I want:
&&hello&&blablabla&&world&&
Any idea? Thanks