正则表达式仅删除数字之间的空格 [英] Regex to remove spaces between numbers only
本文介绍了正则表达式仅删除数字之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需要删除数字之间的空格,以便像这样的字符串:
I need to remove the spaces between numbers only, so that a string like this:
"Hello 111 222 333 World!"
成为
"Hello 111222333 World!"
我已经尝试过了:
message = message.replaceAll("[\\d+](\\s+)[\\d+]", "");
似乎没有完成.
推荐答案
您可以使用基于环视的正则表达式:
You can use this lookaround based regex:
String repl = "Hello 111 222 333 World!".replaceAll("(?<=\\d) +(?=\\d)", "");
//=> Hello 111222333 World!
此正则表达式"(?<=\\d) +(?=\\d)"
确保与前面的数字和后面的数字匹配.
This regex "(?<=\\d) +(?=\\d)"
makes sure to match space that are preceded and followed by a digit.
这篇关于正则表达式仅删除数字之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文