使用正则表达式修剪字符串

时间:2016-04-29 22:31:17

标签: java regex

我不是正则表达式的专家。假设我有这个字符串:

String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."

如果我想删除,normal并将其替换为点,则字符串将变为如下所示:

String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."

我怎样才能在正则表达式中这样做?

非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用,\\w+\\.$这样的正则表达式,其匹配任何以String结尾的,,然后.String.replaceAll(String, String)

String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
        .replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);

输出(按要求)

0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.