简体   繁体   English

如何让字符串替换方法识别例如 R35 不是 R3

[英]How to get String replace-method to recognize for example that R35 is not R3

I have a method that replaces "R3" with a certain value within a string.我有一种方法可以用字符串中的某个值替换“R3”。

But when I have for example a string "R3 R35" which contains "R3" and "R35" and I don't want the "R3" part of "R35" to be replaced with said value, in effect getting "[replacing value]5".但是当我有例如一个包含“R3”和“R35”的字符串“R3 R35”并且我不希望“R35”的“R3”部分被替换为所述值时,实际上得到“[替换值]5"。

I'd like my replace to recognize that R35 is not R3.我希望我的替换能够认识到 R35 不是 R3。 Is there a regex or another way to achieve this?是否有正则表达式或其他方法来实现这一目标?

You could do a regex replacement on \bR3\b :您可以在\bR3\b上进行正则表达式替换:

String input = "R3 R35";
String output = input.replaceAll("\\bR3\\b", "ABC");
System.out.println(output);  // ABC R35

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM