我必须使用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);
}
}
这里如何使用java regex找到“Ts”后跟“kb”..?
答案 0 :(得分:2)