Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regex to retrieve phone numbers

Regex to retrieve phone numbers

Scheduled Pinned Locked Moved C#
regexcomsaleshelptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    Hi I need some help regarding Regex as my knowledge of it is rather poor. I have a string like the following: "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, jane@bleh.com, (H) 041235423234, (W) 1290034589345" What I need to do is to retrieve each Email address as well as each Home and Work number separately. After retrieving the separate values I would then like to store the related ones together as one comma-separated property like in the following example: customer.Email = "john@bleh.com,jane@bleh.com"; customer.HomePhone = "0412623423234,041235423234"; I already used a Regex pattern to retrieve all the email addresses in the string, but it seems to be a bit more tricky with the different phone numbers. Any advice/help?

    H P 2 Replies Last reply
    0
    • E Etienne_123

      Hi I need some help regarding Regex as my knowledge of it is rather poor. I have a string like the following: "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, jane@bleh.com, (H) 041235423234, (W) 1290034589345" What I need to do is to retrieve each Email address as well as each Home and Work number separately. After retrieving the separate values I would then like to store the related ones together as one comma-separated property like in the following example: customer.Email = "john@bleh.com,jane@bleh.com"; customer.HomePhone = "0412623423234,041235423234"; I already used a Regex pattern to retrieve all the email addresses in the string, but it seems to be a bit more tricky with the different phone numbers. Any advice/help?

      H Offline
      H Offline
      Hiren solanki
      wrote on last edited by
      #2

      You may find this solution just made for you Helpful.

      string Text = "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, ";
      Regex HomePhoneRegex = new Regex(@"\(H\)[ ]*\d{13}");
      Regex WorkPhoneRegex = new Regex(@"\(W\)[ ]*\d{13}");
      Regex EmailRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

          String HomePhone = String.Empty;
          String WorkPhone = String.Empty;
          String Email = String.Empty;
      
          if (HomePhoneRegex.Matches(Text).Count == 1)
          {
              HomePhone = HomePhoneRegex.Matches(Text)\[0\].Value.Replace("(H)", String.Empty).Trim();
          }
          if (WorkPhoneRegex.Matches(Text).Count == 1)
          {
              WorkPhone = WorkPhoneRegex.Matches(Text)\[0\].Value.Replace("(W)", String.Empty).Trim();
          }
      
          if (EmailRegex.Matches(Text).Count == 1)
          {
              Email = EmailRegex.Matches(Text)\[0\].ToString();
          }
      

      Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".

      E 1 Reply Last reply
      0
      • H Hiren solanki

        You may find this solution just made for you Helpful.

        string Text = "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, ";
        Regex HomePhoneRegex = new Regex(@"\(H\)[ ]*\d{13}");
        Regex WorkPhoneRegex = new Regex(@"\(W\)[ ]*\d{13}");
        Regex EmailRegex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            String HomePhone = String.Empty;
            String WorkPhone = String.Empty;
            String Email = String.Empty;
        
            if (HomePhoneRegex.Matches(Text).Count == 1)
            {
                HomePhone = HomePhoneRegex.Matches(Text)\[0\].Value.Replace("(H)", String.Empty).Trim();
            }
            if (WorkPhoneRegex.Matches(Text).Count == 1)
            {
                WorkPhone = WorkPhoneRegex.Matches(Text)\[0\].Value.Replace("(W)", String.Empty).Trim();
            }
        
            if (EmailRegex.Matches(Text).Count == 1)
            {
                Email = EmailRegex.Matches(Text)\[0\].ToString();
            }
        

        Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".

        E Offline
        E Offline
        Etienne_123
        wrote on last edited by
        #3

        Thanks that did help :)

        H 1 Reply Last reply
        0
        • E Etienne_123

          Thanks that did help :)

          H Offline
          H Offline
          Hiren solanki
          wrote on last edited by
          #4

          You're welcome. :) Glad it helped you.

          Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".

          E 1 Reply Last reply
          0
          • H Hiren solanki

            You're welcome. :) Glad it helped you.

            Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".

            E Offline
            E Offline
            Etienne_123
            wrote on last edited by
            #5

            Expresso[^] is a rather nifty tool that I found. It's ideal to test you're Regex on some sample text

            H 1 Reply Last reply
            0
            • E Etienne_123

              Expresso[^] is a rather nifty tool that I found. It's ideal to test you're Regex on some sample text

              H Offline
              H Offline
              Hiren solanki
              wrote on last edited by
              #6

              I am using this tool every time I need fun with Regex. :)

              Regards, Hiren. -"I don't know, I don't care, and it doesn't make any difference".

              1 Reply Last reply
              0
              • E Etienne_123

                Hi I need some help regarding Regex as my knowledge of it is rather poor. I have a string like the following: "John Doe, john@bleh.com, (H) 0412623423234, (W) 1290034589345, Jane Doe, jane@bleh.com, (H) 041235423234, (W) 1290034589345" What I need to do is to retrieve each Email address as well as each Home and Work number separately. After retrieving the separate values I would then like to store the related ones together as one comma-separated property like in the following example: customer.Email = "john@bleh.com,jane@bleh.com"; customer.HomePhone = "0412623423234,041235423234"; I already used a Regex pattern to retrieve all the email addresses in the string, but it seems to be a bit more tricky with the different phone numbers. Any advice/help?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                We also have a RegEx forum.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups