I have to convert "Tskb" to "TsKB" using java regex whenever it comes as single word. I have written below code which not working.
我必须使用java正则表达式将“Tskb”转换为“TsKB”,只要它是单个单词。我写下面的代码不起作用。
public class TestBGR {
private static final Pattern s_TsKB = Pattern.compile("/(Ts?.*)(?=.*kb)^(\\w+)$/");
public static void main(String[] args) {
String text = "Tskb";
Matcher matcher = s_TsKB.matcher(text);
StringBuilder builder = new StringBuilder(text);
int offset = 0;
while (matcher.find())
{
String replacement = "KB";
builder.replace(matcher.start() + offset, matcher.end() + offset,
replacement);
offset += replacement.length() - matcher.group().length();
}
System.out.println(builder);
}
}
Here how to find "Ts" followed by "kb" using java regex..?
这里如何使用java正则表达式找到“Ts”后跟“kb”..?
1 个解决方案
#1
2
You can simply do a replaceAll
with
你可以简单地用replaceAll做
(?<=\\bTs)kb\\b
and replace by KB
.The lookbehind will make sure kb
has Ts
before.
并且用KB代替。lookbehind将确保kb之前有Ts。
See demo.
见演示。
https://regex101.com/r/fM9lY3/13
https://regex101.com/r/fM9lY3/13
#1
2
You can simply do a replaceAll
with
你可以简单地用replaceAll做
(?<=\\bTs)kb\\b
and replace by KB
.The lookbehind will make sure kb
has Ts
before.
并且用KB代替。lookbehind将确保kb之前有Ts。
See demo.
见演示。
https://regex101.com/r/fM9lY3/13
https://regex101.com/r/fM9lY3/13