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. Regular Expressions
  4. Regular Expression

Regular Expression

Scheduled Pinned Locked Moved Regular Expressions
regex
7 Posts 5 Posters 20 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.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs

    F P J S 4 Replies Last reply
    0
    • M Morgs Morgan

      Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs

      F Offline
      F Offline
      Firo Atrum Ventus
      wrote on last edited by
      #2

      Just use ? operator Like this:

      ^\d{3}-?\s?\d{3}-?\s?\d{4}$

      You can flame me whichever way you want and I wouldn't care a bit. But if you group me with some idiots, I'll turn into your worst nightmare.

      M P 2 Replies Last reply
      0
      • F Firo Atrum Ventus

        Just use ? operator Like this:

        ^\d{3}-?\s?\d{3}-?\s?\d{4}$

        You can flame me whichever way you want and I wouldn't care a bit. But if you group me with some idiots, I'll turn into your worst nightmare.

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        Thanks a ton Firo, will give it a try... Morgs

        1 Reply Last reply
        0
        • F Firo Atrum Ventus

          Just use ? operator Like this:

          ^\d{3}-?\s?\d{3}-?\s?\d{4}$

          You can flame me whichever way you want and I wouldn't care a bit. But if you group me with some idiots, I'll turn into your worst nightmare.

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

          I think that would match values like: 123456-7890 123456 7890 123456- 7890 123-4567890 123-456 7890 123-456- 7890 123 4567890 123 456-7890 123 456- 7890 123- 4567890 123- 456-7890 123- 456 7890 123- 456- 7890 Which are not in his spec.

          1 Reply Last reply
          0
          • M Morgs Morgan

            Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs

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

            If you want to ensure that the separators (if any) between the groups are the same, I think you need to use a backreference[^]. Something like this perhaps: (?'Area'\d{3})(?(?=(\s|-))(?'Sep'(\s|-)))(?'Prefix'\d{3})(?(Sep)\k<Sep>)(?'Number'\d{4})

            modified on Sunday, July 17, 2011 2:00 PM

            1 Reply Last reply
            0
            • M Morgs Morgan

              Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs

              J Offline
              J Offline
              JimmyRopes
              wrote on last edited by
              #6

              I think this is what you need. ^\d{3}(-|\s)?\d{3}(-|\s)?\d{4}$

              Simply Elegant Designs JimmyRopes Designs
              Think inside the box! ProActive Secure Systems
              I'm on-line therefore I am. JimmyRopes

              1 Reply Last reply
              0
              • M Morgs Morgan

                Hi Guys, I have three Regex and would like to know if it's possible to combine them into one. I'm validating phone numbers in the form: - 0219081234 - 021 908 1234 - 021-908-1234 here are my regex for the above in order: - var phoneRegex1 = /^(\d{3})(\d{3})(\d{4})$/; - var phoneRegex2 = /^(\d{3})(\-)(\d{3})(\-)(\d{4})$/; - var phoneRegex3 = /^(\d{3})(\ )(\d{3})(\ )(\d{4})$/; Thanks in Advance, Morgs

                S Offline
                S Offline
                Shahan Ayyub
                wrote on last edited by
                #7

                Have a look at this pattern: ^\d{3}[\s-]?\d{3}[\s-]?\d{4}$

                modified on Tuesday, August 23, 2011 8:35 PM

                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