删除字符串中的空格和特殊字符(仅数字之间)

问题描述 投票:0回答:1

我是RegEx的新手。我正在尝试在字符串中的数字之间去除所有空格和特殊字符。请注意,字符串可能包含其他带数字的字符。例如,使用此字符串,

String s1 = "This is Sample AmericanExp Card Number 3400 1000 2000 009";

我正在尝试的是-

String s1 = "This is Sample AmericanExp Card Number 3400 1000 2000 009";
String regExp = "[^\\w]+";
String replacement = "";
String changed= s1.replaceAll(regExp, replacement);
System..out.println("changed->"+content);

其输出为ThisisSampleAmericanExpCardNumber340000000000009,所需的输出是“这是样本AmericanExp卡号340010002000009”。感谢帮助,请让我知道其背后的概念。

java regex
1个回答
0
投票

尝试此正则表达式:(?<=[0-9]*)\s(?=[0-9]*)。它使用正向先行和后向搜索,因此它与数字匹配,而无需实际捕获并替换它们。

© www.soinside.com 2019 - 2024. All rights reserved.