简体   繁体   English

Java 正则表达式 - 删除非数字字符

[英]Java Regex - remove non-numeric characters

I new to Regex and I am trying to remove the non-numeric characters from a string using Java's Regex.我是 Regex 的新手,我正在尝试使用 Java 的 Regex 从字符串中删除非数字字符。

The string could have alphabets after the numbers, and white spaces could appear before the numbers, in between the numbers and alphabets, and after the alphabets.字符串可以在数字之后有字母,并且空格可以出现在数字之前、数字和字母之间以及字母之后。

For example, the a valid string could be " -23 asdf" .例如,有效的字符串可以是" -23 asdf"

I have written the following Regex: "(\\s*)[^-?][^0-9][\\s*a-zA-Z.\\s*]" , and I'm using replaceAll method to get rid of non-numeric characters replaceAll(regex, "")我写了以下正则表达式: "(\\s*)[^-?][^0-9][\\s*a-zA-Z.\\s*]" ,我正在使用 replaceAll 方法摆脱非数字字符replaceAll(regex, "")

My thought was that \\s* matches zero or more times of the space character我的想法是\\s*匹配零次或多次空格字符
[^-?] retains the optional minus sign [^-?]保留可选的减号
[^0-9] retains the numeric characters [^0-9]保留数字字符
[\\s*a-zA-Z.\\s*] matches white space before and after any alpha characters.\ [\\s*a-zA-Z.\\s*]匹配任何字母字符前后的空格。\

However, when I try to run this code with input of " -23 asdf" , I get an unexpected result of -2但是,当我尝试使用输入" -23 asdf"运行此代码时,我得到了-2的意外结果

What am I doing wrong?我究竟做错了什么?

Here is one viable approach:这是一种可行的方法:

String input = "hello world -23 as-df blah blah blah";
String output = input.replaceAll("[^\\d-]|-(?=\\D)", "");
System.out.println(output);

This prints:这打印:

-23

The regex used here says to match:此处使用的正则表达式表示匹配:

[^\\d-]   any non numeric character EXCEPT for dash (i.e. spare -)
|         OR
-(?=\\D)  remove dash if NOT followed by a number

That is, we do strip off dash if it not be part of a negative number.也就是说,如果破折号不是负数的一部分,我们会去掉它。 The replacement is empty string, to remove this unwanted content.替换为空字符串,以删除此不需要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM