2

I new to Regex and I am trying to remove the non-numeric characters from a string using Java's 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".

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, "")

My thought was that \\s* matches zero or more times of the space character
[^-?] retains the optional minus sign
[^0-9] retains the numeric characters
[\\s*a-zA-Z.\\s*] matches white space before and after any alpha characters.\

However, when I try to run this code with input of " -23 asdf", I get an unexpected result of -2

What am I doing wrong?

user101998
  • 241
  • 5
  • 15
  • https://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters Should help you. – Arundeep Chohan Dec 17 '20 at 02:34
  • 1
    I think `[^0-9]` only retains one character, you need to add `+` – Keale Dec 17 '20 at 02:36
  • `[^-?]` is incorrect. You want `-?` without any brackets. The `?` is not a special character when it appears inside the brackets. The brackets mean the digits must be preceded by one character which is neither a hyphen nor a question mark. – VGR Dec 17 '20 at 03:02

1 Answers1

1

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • May I ask what does the equal sign do here in `-(?=\\D)`? Also for the parenthesis, I thought it means grouping? Is that true in this context? Thanks! – user101998 Dec 17 '20 at 03:03
  • 1
    @user101998 `(?=\\D)` means _look ahead_ and assert that what follows is _not_ a number. That is match `-` only when it is _not_ a negative sign followed by some number. – Tim Biegeleisen Dec 17 '20 at 03:04