【发布时间】:2018-09-07 01:37:22
【问题描述】:
我正在尝试使用正则表达式格式化字符串,如下所示:
String string = "5.07+12.0+2.14";
string = string.replaceAll("\\.0[^0-9]","");
我认为会发生的是字符串会变成:
5.07+122.14 //the regex will delete the .0+ next to the 12
如何创建正则表达式,以便它只删除 .0 而不是 + 号?
我更愿意在同一个调用中完成所有操作来“replaceAll”
感谢任何建议
【问题讨论】:
-
为什么 5.07 会变成 57,因为 .0 后面跟着一个数字 @CertainPerformance
-
或者你可以使用负前瞻
\.0(?!\d)
标签: java regex replaceall