[英]Java regex to remove space between numbers only
我有一些像这样的字符串:
String a = "Hello 12 2 3 4 45th World!";
我想将数字连接为: "Hello 12234 45th World"
通过尝试a.replaceAll("(?<=\\\\d) +(?=\\\\d)", "")
,我得到的结果是: "Hello 1223445th World"
。
有什么办法只能连接数字,而不是数字 + th ?
您只需要在预读中添加单词边界即可将数字匹配为整个单词:
a.replaceAll("(?<=\\d) +(?=\\d+\\b)", ""))
String a = "Hello 12 2 3 4 45th World!";
System.out.println(a.replaceAll("(?<=\\d) +(?=\\d+\\b)", ""));
输出:
Hello 12234 45th World!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.