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.
我需要在>字符之后用§r§a替换所有§r。
It should be
它应该是
O > O §o TEXT §r§a §o TEXT §r§a
as the result.
作为结果。
I tried >*(\§r) regex but it ignores >.
我尝试了> *(\§r)正则表达式,但它忽略了>。
May You point on my error?
你能指出我的错误吗?
3 个解决方案
#1
1
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.
使用纯正则表达式执行此操作将很困难。
#2
1
Description
((?:(?!>).)*>.*?|)(§r)
Replace With: $1§r§a
替换为:$1§r§a
** To see the image better, simply right click the image and select view in new window
**要更好地查看图像,只需右键单击图像并在新窗口中选择视图即可
Example
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
Explanation
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
----------------------------------------------------------------------
#3
0
A pure regex way is by using \G
like following
纯正的正则表达方式是使用\ G,如下所示
(\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"));
#1
1
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.
使用纯正则表达式执行此操作将很困难。
#2
1
Description
((?:(?!>).)*>.*?|)(§r)
Replace With: $1§r§a
替换为:$1§r§a
** To see the image better, simply right click the image and select view in new window
**要更好地查看图像,只需右键单击图像并在新窗口中选择视图即可
Example
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
Explanation
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
----------------------------------------------------------------------
#3
0
A pure regex way is by using \G
like following
纯正的正则表达方式是使用\ G,如下所示
(\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"));