Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a regex to remove everything except numbers

Tags:

I need something besides [^0-9\n], I want a regex(but dont know how to make one), that captures anything in a pattern of numbers like this, "0000000000" or "000-000-0000" or basically any numbers that exist with spaces and or special characters right before or in between.

so any number, even like these (626*) 34a2- 4387) should convert to 6263424387

How can this be accomoplished? Im thinking its too hard?

like image 201
user3283015 Avatar asked Jan 05 '15 02:01

user3283015


People also ask

How do you delete everything from a string except numbers?

To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.

How do I select only numbers in regex?

If you want to get only digits using REGEXP, use the following regular expression( ^[0-9]*$) in where clause. Case 1 − If you want only those rows which have exactly 10 digits and all must be only digit, use the below regular expression.

How do I exclude a regex match?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.


1 Answers

You can search for all non-digits using:

\D+

OR

[^0-9]+

And replace by empty string.

RegEx Demo

like image 51
anubhava Avatar answered Sep 18 '22 17:09

anubhava