Extract only numbers from String

Hello All,

I have a requirement where I have to update valid phone numbers from bulk users.

To test if it valid, I have to check the length of only the numerals (it should 11 to 12 max)

For ex:

$a = (+1) 201 839 8419

In the above example all the special characters and space has to be removed (final result should be 12018398419), I don't want to use multiple replace command and want to do it through regex, could you please help??

The below command is not working

string result = Regex.Replace($b, @"[^\d]","");

Any help pl

May 19th, 2015 2:04pm

$a = '(+1) 201 839 8419'
$result = $a -replace "[()\s+]", ""
$result

#Results
12018398419

Free Windows Admin Tool Kit Click here and download it now
May 19th, 2015 2:43pm

This is another approach.

 (' (+1) 201 839 8419'.toCharArray()|%{if($_ -Match '\d'){$_}}) -Join ""

May 20th, 2015 12:30am

The code below (with RegEx) should do what you want::

 

$a = "(+1) 201 839 8419";

$PhoneNumber = $a -replace "\D" , "" # The first parameter is a Regex.

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 3:21am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics