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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:34:10+00:00 2026-06-18T07:34:10+00:00

Possible Duplicate: Regular expression to match phone numbers with country codes Currently I have

  • 0

Possible Duplicate:
Regular expression to match phone numbers with country codes

Currently I have this regex do validate/capture phone numbers: ^(\+?(?<country>\d{1,3}?) ?)?(\(?0?(?<area>\d{2})\)? ?)?(?<phone>9?\d{4}[-. ]?\d{4})$

It accepts numbers like 88888888, 8888-8888, 8888.8888, 11 8888-8888, (11) 88888888, +22 (21) 8888-8888 and every combination in this format.

The problem is: when I pass the area code without contry code (like this: (11) 8888-8888 or 11 8888-8888 or even 1188888888) it gets the area code part (11) as the country code, and leaves the area code empty.

How could I do something like country + area + phone ORarea + phone but NOT country + phone? Without doing something like this:

^(((\(?0?(?<area>\d{2})\)? ?)?|(\+?(?<country>\d{1,3}?) ?)?(\(?0?(?<area>\d{2})\)? ?)?)(?<phone>9?\d{4}[-. ]?\d{4})$

Wich is (area+phone|country+area+phone) but the area and the country regexp gets repeated twice.

  • 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-06-18T07:34:11+00:00Added an answer on June 18, 2026 at 7:34 am

    In fact there is an ambiguity between a country code with two digits and an area code with two digits. When the regexp hits this case, it considers the area code as a country code since country code is placed first and area code is optional.

    SOLUTION #1 : Using a negative lookahead (?!…)

    To solve this problem, one could tell the regexp to ignore two digits country code followed by a phone number. In order to keep the regexp small enough (thus not too complicated), it’s possible to give just the beginning of a phone number.

    Here is the negative lookahead to use: (?!9?\d{4})

    A two digits number is qualified as a country code ONLY if it is not followed by the start of a phone number (9?\d{4}).

    The final regexp is :

    ^(\+?(?<country>\d{1,3}?) ?(?!9?\d{4}))?(\(?0?(?<area>\d{2})\)? ?)?(?<phone>9?\d{4}[-. ]?\d{4})$
    

    SOLUTION #2: Templating the regular expression

    IMO, if a flavor could allow to build a regular expression with templates, it would be a powerful feature. Well this feature doesn’t exist in any flavor I know of.

    But there is a workaround. The following code is a snippet written in C#

    (...)
    string country = "(\\+?(?<country>\\d{1,3}?) ?)";
    string area = "(\\(?0?(?<area>\\d{2})\\)? ?)";
    string phone = "(?<phone>9?\\d{4}[-. ]?\\d{4})";
    string final_regexp = String.Format("^{1}{2}{3}|{2}?{3}$", country, area, phone);
    (...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Regex to match URL I wrote a regular expression to validate only
Possible Duplicate: Regular expression to match US phone numbers I need to find phone
Possible Duplicate: Regex to match URL Is there a regular expression to return a
Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
Possible Duplicate: Using a regular expression to validate an email address I have just
Possible Duplicate: What is a good regular expression to match a URL? I have
Possible Duplicate: Regular expression to match numbers with or without commas and decimals in
Possible Duplicate: Regular Expression to match outer brackets I have a string of the
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have this list
Possible Duplicate: Regular expression: match all words except I need your help for using

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.