Reputation: 13
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?
Upvotes: 1
Views: 110
Reputation: 11032
A pure regex way is by using \G
like following
(\G(?!\A)|>)(.*?)§r
and, replace with
$1$2§r§a
Java Code
System.out.println("O§r §r > O §o TEXT §r §o §r TEXT §r".replaceAll("(\\G(?!\\A)|>)(.*?)§r", "$1$2§r§a"));
Upvotes: 0
Reputation: 15000
((?:(?!>).)*>.*?|)(§r)
Replace With: $1§r§a
** To see the image better, simply right click the image and select view in new window
Live Demo
https://regex101.com/r/xP8dI5/1
Sample text
§r O > O §o TEXT §r §o TEXT §r
After Replacment
§r O > O §o TEXT §r§a §o TEXT §r§a
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
§r '§r'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
Upvotes: 1
Reputation: 4289
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.
Upvotes: 1