我不了解replaceAll方法的工作原理。更具体地说,第一个参数采用字符串正则表达式。我想删除所有不是数字的字符,包括句点。
我的实施。
userId = inputRow.next().replaceAll("[\\.^\\d.]", "");
示例输出:
"","",""
"","",""
"","",""
"","BBLDX",""
"","N",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
删除除字母之外的所有内容
答案 0 :(得分:1)
正则表达式中有错误。
它应该是" [^ \\ d]"。
这将替换所有不是数字的字符。
答案 1 :(得分:1)
你可以这样做:
userId = inputRow.next().replaceAll("[^\\d]", "");
[^ \\ d]将匹配所有不是数字的字符序列,并将其替换为空字符串。
答案 2 :(得分:0)
String text = "afadsfas1234fasdfadsf.fasdf.sadfadsf.afadsf.5678";
System.out.println(text);
// /D is equal to a non digit. [^0-9]
String result = text.replaceAll("[\\.\\D]", "");
System.out.println(result);
//Result will be 12345678
以上代码将帮助您删除所有不是数字的字符,包括句点。 \ d表示数字0-9,\ D表示非数字数字。