J. Doe J. Doe - 1 year ago 75
Java Question

Replace specific character sequence after specific character

I have the folowing string for example:
O > O §o TEXT §r §o TEXT §r

I need to replace all "§r" with "§r§a" only after ">" character.

It should be
O > O §o TEXT §r§a §o TEXT §r§a
as the result.

I tried >*(\§r) regex but it ignores >.

May You point on my error?

Answer Source

The easiest way to do this would be to split it into two strings first and then run a replace. That is, you could take

int index = inputString.indexOf('>') + 1;
String first = inputString.subString(0, index);
String second = inputString.subString(index);
String finalString = first + second.replace("§r", "§r§a");

Doing this with a pure regular expression would be difficult.