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 3939658
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:19:33+00:00 2026-05-20T00:19:33+00:00

Possible Duplicate: A comprehensive regex for phone number validation I have an unformatted phone

  • 0

Possible Duplicate:
A comprehensive regex for phone number validation

I have an unformatted phone number (guaranteed to be 10 digits) and an unformatted extension (could be null, blank or any number of numbers). I need to put them together into a “friendly” string. I thought I’d concatenate them, then format the concatenation using a Regex.Replace. Here’s the unit test I’m using to try various regexes before I plug one in:

    [Test, Ignore("Sandbox, does not test production code")]
    public void TestPhoneRegex()
    {
        string number = "1234567890";
        string extension = "";

        var formattedContactNumber =
            Regex.Replace("{0} x{1}".FormatWith(number, extension),
                          @"^(\d{3})[ -]?(\d{3})[ -]?(\d{4})( x\d+)?",
                          @"$1-$2-$3$4");

        Debug.WriteLine("{0} x{1}".FormatWith(number, extension));
        Debug.WriteLine(formattedContactNumber);

        Assert.AreEqual("123-456-7890", formattedContactNumber);
    }

The expected formatted string is the formatted phone number, without the “x” and extension. However, the last capture group is matching the “x” with or without a number behind it, so instead of “123-456-7890” I get “123-456-7890 x”. This is the last bit of development that needs to be tied down before a release. Help?

  • 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-20T00:19:34+00:00Added an answer on May 20, 2026 at 12:19 am

    x isn’t matched by your regex, so it isn’t replaced put of the string. Try this regex instead:

    @"^(\d{3})[ -]?(\d{3})[ -]?(\d{4}) x(\d*)
    

    In the new regex x isn’t optional – it will always be there according to your code (If you do want it to be optional you can use ?x?(\d*)). Also, we’re using \d*, so make sure the last group will always match, even when it’s empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: A comprehensive regex for phone number validation Greetings, I need to implement
Possible Duplicate: A comprehensive regex for phone number validation How to validate a phone
Possible Duplicate: A comprehensive regex for phone number validation Just a simple regex required
Possible Duplicate: A comprehensive regex for phone number validation Validate phone number with JavaScript
Possible Duplicate: A comprehensive regex for phone number validation Perhaps a forgiving JavaScript solution
Possible Duplicate: A comprehensive regex for phone number validation i want to validate a
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: simple regex in php, formatting decimal number How to convert a digit
Possible Duplicate: css rule to disable text selection highlighting On my homepage i have
Possible Duplicate: Symfony2 AJAX Login I have implemented a custom authentication handler service to

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.