-2

I have a string "Home 333-666-8888 Do Not Disturb" and want to extract the numbers from the string. For that I found a solution with ReplaceAll functionality in Java.

String ab="Home   333-666-8888  Do Not Disturb";

String bc=ab.replaceAll("[^0-9-]", "");

And it gives me 333-666-8888

My concern is how does replaceAll functionality actually works? I used to assume that it would replace the things matching the Regular Expression with blank and return me Home Do Not Disturb. But It gives me 333-666-8888. Can anyone help me in understanding how replaceAll with Regular Expression Works?

  • 1
    What does the `^` mean in a regular expression? – Joe C Sep 16 '17 at 15:20
  • 1
    Voting to close on the grounds that your issue is down to a typo, and thus not likely to be helpful to future readers. – Joe C Sep 16 '17 at 15:21
  • ^ is basically a start of string in Regular Expression – NewGeekInTown Sep 16 '17 at 15:26
  • My question is something else. Can you please help. How is that expression exactly working @Dukeling – NewGeekInTown Sep 16 '17 at 15:32
  • 1
    If you know what replaceAll does on a high level (replace all occurrences of the thing on the left with the thing on the right), and you read the post I linked above (which indicates that `^` is **not** the start of the string as you used it there), you should read `replaceAll("[^0-9-]", "")` as "Replace all characters that are **not** `0-9` or `-` with an empty string", which is exactly what the code is doing. – Bernhard Barker Sep 16 '17 at 15:37
  • Thanks alot @Dukeling that explanation helped alot... :) – NewGeekInTown Sep 16 '17 at 15:40

2 Answers2

1

You think that ^ means to match the start of the string, right? Well, the fact is, it does match the start of the string, except when placed in a character class. If it is inside a character class ([]), it means "anything except the characters in this character class". Therefore, your regex actually matches everything except the phone number part in your string, and replaced everything except the phone number part with empty strings!

Even if it did mean to match the start of the string, it would not have worked as you have expected. Because your string does not start with 0-9-.

So to solve this, just remove the ^.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Your understanding about the replaceAll method is correct Try this :

String ab = "333-666-8888";

String bc=ab.replaceAll("[0-9-]", "");