This will do it.

(\d\d\d-?\s?\d\d\d-?\s?\d\d\d\d\s?)?(x\d\d\d\d)?

or shorter equivalent:

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

You want to match the full phone number, optionally with space/dash, and make that whole thing optional, then include extension, and make that optional too.

Answer from Samuel Neff on Stack Overflow
🌐
GitHub
gist.github.com › 11458504
Regular Expressions for U.S. Phone Numbers · GitHub
Regular Expressions for U.S. Phone Numbers. GitHub Gist: instantly share code, notes, and snippets.
🌐
Regex Tester
regextester.com › 103298
Phone Number plus optional international code and extension number - Regex Tester/Debugger
Regex Tester is a tool to learn, build, & test Regular Expressions (RegEx / RegExp). Results update in real-time as you type. Roll over a match or expression for details. Save & share expressions with others. Explore the Library for help & examples. Undo & Redo with {{getCtrlKey()}}-Z / Y. Search for & rate Community patterns. ... extended (x) extra (X) single line (s) unicode (u) Ungreedy (U) Anchored (A) dup subpattern names(J) Phone Number with optional international code and extension number
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › validate-phone-numbers-with-country-code-extension-using-regular-expression
Validate Phone Numbers ( with Country Code extension) using Regular Expression - GeeksforGeeks
July 23, 2025 - Rules for the valid phone numbers are: ... It should be followed by Country code and National number. It may contain white spaces or a hyphen ( - ). the length of phone numbers may vary from 7 digits to 15 digits.
🌐
Snipplr
snipplr.com › view › 5407 › us-phone-number-with-extension-flexible
US Phone Number with extension, flexible - Regular Expression Snipplr Social Repository
March 13, 2008 - ^((1|+1)?( |-|.)?)?((?[0-9]{3})?|[0-9]{3})( |-|.)?([a-zA-Z0-9]{3}( |-|.)?[a-zA-Z0-9]{4})[ ]*(( |x|ext|ext.|extension|Ext|Ext.|Extension|#){1}[ ]?([0-9]){1,7}){0,1}$
🌐
regex101
regex101.com › library › cFEhad
regex101: US Phone number regex
Search string for use in contact cleaning project $1 = intenational code, omitting preceding "+" $2 = area code $3 = first three digits of phone number $4 = last four digits of phone $5 = extension, inclusive of any commas or hashes to automate ...
🌐
GitHub
gist.github.com › jacurtis › ccb9ad32664d3b894c12
Most Useful Regex's · GitHub
For example +80 9800 98 91 or (801) 112-1123 or +12 43 42 68 34 83 or even just lazy number typers who just use numbers like 8011121122 or 801-112-1122 or 801.112.1122 * Probably my most frequently used Regex of all time.
Find elsewhere
Top answer
1 of 4
3

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.

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:

2 of 4
1

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   
🌐
UI Bakery
uibakery.io › regex-library › phone-number
Phone number regex
The regular expressions below can be used to validate if a string is a valid phone number format and to extract a phone number from a string. Please note that this validation can not tell if a phone number actually exists. ... A simple regex to validate string against a valid international phone number format without delimiters and with an optional plus sign:
🌐
devcraft
devally.dev › home › regex library › us phone number
Regex for US Phone Number — Pattern, Examples & Variations | devcraft
It handles formats like (555) 123-4567, 555-123-4567, 5551234567, and +1 555 123 4567. The pattern is flexible enough for user input while still enforcing the 10-digit US format. This pattern is commonly used for phone number form validation and contact data extraction.
🌐
GitHub
gist.github.com › jengle-dev › a327f2b58e08f384ec41dbb5e5454c28
RegEx Phone Number Matching & Validation · GitHub
This 'Regular Expression' (RegEx) is used to match and validate US phone numbers in the following formats, where X represents digits (d): ... Another Example with additional parameters on limiting input to 0-9: \(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ...
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9781449327453 › ch04s02.html
4.2. Validate and Format North American Phone Numbers - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - A regular expression can easily check whether a user entered something that looks like a valid phone number. By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want. ^\(?([0-9]{3})\)?[-.●]?([0-9]{3})[-.●]?([0-9]{4})$ ... Regex phoneRegex = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"); if (phoneRegex.IsMatch(subjectString)) { string formattedPhoneNumber = phoneRegex.Replace(subjectString, "($1) $2-$3"); } else { // Invalid phone number }
Authors   Jan GoyvaertsSteven Levithan
Published   2012
Pages   609
🌐
regex101
regex101.com › library › lV0xJ7
regex101: phone w/ extension
Search, filter and view user submitted regular expressions in the regex library. Over 20,000 entries, and counting!
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Regex for US phone number - JavaScript
December 3, 2021 - Tell us what’s happening: Describe your issue in detail here. **Your code so far** function telephoneCheck(str) { return /^[\+]?([0-9][\s]?|[0-9]?)([(][0-9]{3}[)][\s]?|[0-9]{3}[-\s\.]?)[0-9]{3}[-\s\.]?[0-9]{4,6}$/ig.test(str); } telephoneCheck("555-555-5555"); **Your browser information:** User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Challenge: Telephone Number Validator Link to the challenge...
🌐
Regex Tester
regextester.com › 103299
Phone Number plus optional int. code and ext. number - Regex Tester/Debugger
Regular Expression to Rexed patter to validate a regular 10 digits phone number plus optional 1 to 3 digits international code and extension number (any number of digits)
🌐
RegExr
regexr.com › 3c53v
Phone Number regex
Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details. Validate patterns with suites of Tests. Save & share expressions with others. Use Tools to explore your results.
🌐
Avaya
documentation.avaya.com › bundle › IPOfficeMSTeamsDirectRouting › page › Telephone_Number_Examples.html
Regex Telephone Number Examples
Skip to main contentSkip to search · Powered by Zoomin Software. For more details please contactZoomin · Log in to get a better experience · Login · Stay Connected · SitemapTerms of UsePrivacyCookiesTrademarksAccessibility