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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:54:44+00:00 2026-06-14T21:54:44+00:00

Possible Duplicate: Regular expression to match US phone numbers I need to find phone

  • 0

Possible Duplicate:
Regular expression to match US phone numbers

I need to find phone numbers in html, i have seen many examples here and on google but not sure why i cannot get any one to work , it simply wont find the number .Suppose html is :

  1. example site 1 for phone number
  2. example site 2 for phone number

Basically i was going for all US pattern phone numbers, but any thing i found i used it but no luck i am using this code:

CODE:
public static string Extractphone(string html)
{
StringBuilder sb = new StringBuilder();

    try
    {
        List<string> tmpemail = new List<string>();
        string data = html; 
        //instantiate with this pattern 
        Regex emailRegex = new Regex(@"(\\d{3})-(\\d{3})-(\\d{4})",
            RegexOptions.IgnoreCase);
        //find items that matches with our pattern
        MatchCollection emailMatches = emailRegex.Matches(data);

        foreach (Match emailMatch in emailMatches)
        {
            if (!tmpemail.Contains(emailMatch.Value.ToLower()))
            {
                sb.AppendLine(emailMatch.Value.ToLower());

                tmpemail.Add(emailMatch.Value.ToLower());
            }
          //  (541) 708-1364
        }
        //store to file
    }
    catch (Exception ex)
    {
    }
    return sb.ToString();
}

I have changed the pattern many times from many examples but no luck.

  • 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-14T21:54:45+00:00Added an answer on June 14, 2026 at 9:54 pm
    • You are using a string literal so your '\\' is not escaping the backslash. Just removing the extra slash will get you to match your first case
    • To handle multiple cases you have to put those multiple cases into the regex. Since you might have a leading parenthsis you have to check for it by having \(?. The same with the trailing one you may have that and 0+ spaces or the dash so you need to check the or case so instead of just – you need (\)\s*|-)
    • You don’t need parens around the \d{3} or\d{4} groups as it is a single match. That is probably just making the expression harder to read and understand

    So that leaves you with the following for your Regex initialization

    Regex emailRegex = new Regex(@"\(?\d{3}(\)\s*|-)\d{3}-\d{4}",
                RegexOptions.IgnoreCase);
    

    I haven’t tested this robustly but I think that works.

    As a side note regular expressions are one of those things that are really cryptic if you don’t understand them. Trying to just take someone else’s expression and use it can give poor results if you don’t actually understand what is being checked for in the expression. Also what I wrote there is not comprehensive. It would only be useful in those two cases. To be able to handle any phone number the expression quickly gets much more complicated.

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

Sidebar

Related Questions

Possible Duplicate: Regular expression: match all words except I need your help for using
Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
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 to match URL Is there a regular expression to return a
Possible Duplicate: php regex , extract phone number from text/html I have following requirement.
Possible Duplicate: regular expression for letters, numbers and - _ I am creating a
Possible Duplicate: regular expression for letters, numbers and - _ I would like to
Possible Duplicate: Regular Expression for Phone Number I'm kinda new to regular expressions, so

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.