Chun ping Wang
Chun ping Wang

Reputation: 3929

Remove formatting from US phone number and their extension number

HI need help get phone number and there extension using either replace or regex

phone
(123) 455-6789 --> 1234556789
(123) 577-2145 ext81245 --> 1235772145

extension
(123) 455-6789 --> 
(123) 577-2145 ext81245 --> 81245
"(123) 455-6789" -replace "[()\s\s-]+|Ext\S+", "" "(123) 455-6789 Ext 2445" -replace "[()\s\s-]+|Ext\S+", ""

This solves phone number but not extension.

Upvotes: 2

Views: 468

Answers (4)

JvdV
JvdV

Reputation: 75900

So alternatively, you could use two replace functions in a single go. Say your original data sits in File1.txt and you want to output to File2.txt then you could use:

enter image description here

$content = Get-Content -Path 'C:\File1.txt'
$newContent = $content -replace '[^\d\n]', '' -replace '^(.{10})(.*)', 'Phone: $1   Extention: $2'
$newContent | Set-Content -Path 'C:\File2.txt'

enter image description here

Upvotes: 0

user7571182
user7571182

Reputation:

You may try:

^\((\d{3})\)\s*(\d{3})-(\d{4})(?: ext(\d{5}))?$

Explanation of the above regex:

  • ^, $ - Represents start and end of the line respectively.

  • \((\d{3})\) - Represents first capturing group matching the digits inside ().

  • \s* - Matches a white-space character zero or more times.

  • (\d{3})- - Represents second capturing group capturing exactly 3 digits followed by a -.

  • (\d{4}) - Represents third capturing group matching the digits exactly 4 times.

  • (?: ext(\d{5}))? -

    • (?: Represents a non capturing group
    • ext - Followed by a space and literal ext.
    • (\d{5}) - Represents digits exactly 5 times.
    • ) - Closing of the non-captured group.
    • ? - Represents the quantifier making the whole non-captured group optional.

Pictorial Representation

You can find the sample demo of the above regex in here.

Powershell Commands:

PS C:\Path\To\MyDesktop> $input_path='C:\Path\To\MyDesktop\InputFile.txt'
PS C:\Path\To\MyDesktop> $output_path='C:\Path\To\MyDesktop\outFile.txt'
PS C:\Path\To\MyDesktop> $regex='^\((\d{3})\)\s*(\d{3})-(\d{4})(?: ext(\d{5}))?$'
PS C:\Path\To\MyDesktop> select-string -Path $input_path -Pattern $regex -AllMatches | % { "Phone Number: $($_.matches.groups[1])$($_.matches.groups[2])$($_.matches.groups[3])             Extension: $($_.matches.groups[4])" } > $output_path

Sample Result: Result

Upvotes: 3

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58491

After you've replaced all characters, you could split the result to get two numbers

Applied to your example

@(
'(123) 455-6789'
 , '(123) 577-2145 ext81245'
) | % {
    $elements = $_ -replace '[()\s\s-]+' -split 'ext' 
    [PSCustomObject]@{
        phone = $elements[0]
        extension = $elements[1]
    }
}

returns

phone      extension
------     ---------
1234556789          
1235772145 81245   

Upvotes: 1

Bren
Bren

Reputation: 615

Try out this pattern. It will match phone numbers with and without parentheses, spaces and hyphens.

((?:\(?)(\d{3})(?:\)?\s?)(\d{3})(?:-?)(\d{4}))

Upvotes: 0

Related Questions