Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:34:35+00:00 2026-05-30T03:34:35+00:00

I need help writing regular experssion that will match phone numbers in some data.

  • 0

I need help writing regular experssion that will match phone numbers in some data. Somin the following format:

XXX-XXX-XXXX
XXX-XXX-XXXX Ext. XXX
XXX-XXX-XXXX Ext. XXXX
(XXX) XXX-XXXX
XXX-XXX-XXXX (Text)
XXX.XXX.XXXX

and one regexp specifically for this line:

XXX-XXX-XXXX (Text & Special Chars.); XXX-XXX-XXXX (Text)

I’ve searched through several questions and tried the general 0-9 match but to no avail so far. I also tried this more recently to fetch normal phone numbers:

preg_match_all('/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/', $a3, $n);

print_r($n);

Can someone show me the right way? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:34:37+00:00Added an answer on May 30, 2026 at 3:34 am

    This pattern will match the examples you provided in any text.

    ((?:\d{3}[.-]|\(\d{3}\)\s+)\d{3}[.-]\d{4})(?:\s+Ext\.\s+(\d+)|\s+\(.*?\))?

    If you can provide some sample data or explain in more detail what the subject looks like the pattern can likely be improved. For example, are all the numbers on a separate lines (like your pattern would suggest) or are they part of a text?


    Example script, and the pattern explained:

    $string = <<<EOT
    123-456-7890
    
        123-456-7890 Ext. 321
    123-456-7890 Ext. 4321
    (123) 456-7890
    lorem ipsum
    123-456-7890 (Text)
    123.456.7890
    foo bar baz
    EOT;
    
    //preg_match_all("/((?:\d{3}[.-]|\(\d{3}\)\s+)\d{3}[.-]\d{4})(?:\s+Ext\.\s+(\d+)|\s+\(.*?\))?/i", $string, $matches);
    preg_match_all("/
                   (                        # open capturing group to hold the phone number
                       (?:                  # open non-capturing group
                           \d{3}[.-]        # match 3 digits followed by a . or -
                           |                # OR, if the previous part of this group did not match
                           \(\d{3}\)\s+     # match 3 digits between parentheses folowed by one or more spaces
                       )                    # close non-capturing group
                       \d{3}[.-]            # match 3 digits followed by a . or -
                       \d{4}                # match 4 digits
                   )                        # close capturing group
                   (?:                      # open non-capturing group
                       \s+Ext\.\s+          # one or more spaces followed by Ext. followed by one or more spaces
                       (                    # open capturing group to hold the extension number
                           \d+              # match one or more digits
                       )                    # close capturing group
                       |                    # OR, if the previous part of this non-capturing group did not match
                       \s+\(.*?\)           # one or more spaces and then anything, except newline, between (the smallest) pair of parentheses
                   )?                       # close non-capturing group, the ? makes the whole non-capturing group optional
                   /ix", $string, $matches);# the i flag makes the matches case insensitive and x allows for the comments and spacing
    
    echo "<pre>\n";
    print_r($matches);
    

    output:

    Array
    (
        [0] => Array
            (
                [0] => 123-456-7890
                [1] => 123-456-7890 Ext. 321
                [2] => 123-456-7890 Ext. 4321
                [3] => (123) 456-7890
                [4] => 123-456-7890 (Text)
                [5] => 123.456.7890
            )
    
        [1] => Array
            (
                [0] => 123-456-7890
                [1] => 123-456-7890
                [2] => 123-456-7890
                [3] => (123) 456-7890
                [4] => 123-456-7890
                [5] => 123.456.7890
            )
    
        [2] => Array
            (
                [0] => 
                [1] => 321
                [2] => 4321
                [3] => 
                [4] => 
                [5] => 
            )
    
    )
    

    and for the other line, the pattern above will also match that one, but if you do need it here it is

    $string = "123-456-7890 (Text & Special Chars.); 123-456-7890 (Text)";
    
    preg_match_all("/(\d{3}[.-]\d{3}[.-]\d{4})\s+\(.*\);\s+(\d{3}[.-]\d{3}[.-]\d{4})\s+\(.*?\)/i", $string, $matches);
    
    echo "<pre>\n";
    print_r($matches);
    

    output:

    Array
    (
        [0] => Array
            (
                [0] => 123-456-7890 (Text & Special Chars.); 123-456-7890 (Text)
            )
    
        [1] => Array
            (
                [0] => 123-456-7890
            )
    
        [2] => Array
            (
                [0] => 123-456-7890
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help writing a T-SQL query that will generate 52 rows of data
I need some help writing a regular expression that matches this: an opening square
I need some help writing a regex. I have the following strings, xxx.yyy.wwwwwaaa_IN_123 xxx.rrrttttt_IN_12355
I need help writing a regex for a phone number pattern that allows the
I need help writing a query that will validate a first expression and than,
I need help writing a regex with grouping to match the following six input
I am making NO headway in writing a regular expression and need some help.
Need help writing a regular expression to match any class containing the phrase block-container
I need help writing select statement that will do an average of the most
Need help writing a script downloads data from google insight using c# this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.