Regex number and hyphenHow to write a regex for digits separated by - in pythonWhat is the best regular expression to check if a string is a valid URL?Regex for numbers onlyRegular Expression for alphanumeric and underscoresRegular expression to match a line that doesn't contain a wordHow do you access the matched groups in a JavaScript regular expression?How do you use a variable in a regular expression?How to negate specific word in regex?How to match “anything up until this sequence of characters” in a regular expression?Regex - Should hyphens be escaped?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

Where can I get macOS Catalina Beta version?

Meaning of もてり and use of が

Skipping over failed imports until they are needed (if ever)

Are there any features that help with the roll to avoid the destruction of a Wand of Fireballs when using the last charge?

Does the Pi 4 resolve the Ethernet+USB bottleneck issue of past versions?

Can you sign using a digital signature itself?

Can the passive "être + verbe" sometimes mean the past?

Being paid less than a "junior" colleague

How can a valley surrounded by mountains be fertile and rainy?

Why are there so many religions and gods?

Can the UK Prime Minister immediately withdraw the country from the EU without backing from parliament?

Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?

Can a police officer film me on their personal device in my own home?

How can I reduce the sound of rain on a range hood vent?

Reverse of diffraction

What does grep -v "grep" mean and do?

Do space suits measure "methane" levels or other biological gases?

Is it bad to describe a character long after their introduction?

Generate and graph the Recamán Sequence

Can 'leave' mean 'forget'?

Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?

Question on oracles

3D nonogram, beginner's edition

In the context of a differentiator circuit, what is a “current-sensing resistor”?



Regex number and hyphen


How to write a regex for digits separated by - in pythonWhat is the best regular expression to check if a string is a valid URL?Regex for numbers onlyRegular Expression for alphanumeric and underscoresRegular expression to match a line that doesn't contain a wordHow do you access the matched groups in a JavaScript regular expression?How do you use a variable in a regular expression?How to negate specific word in regex?How to match “anything up until this sequence of characters” in a regular expression?Regex - Should hyphens be escaped?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I'm trying to match number with regular expression like:



34-7878-3523-4233


with this:



^[0-9][0-9-]*-[0-9-]*[0-9]$


But the expression also allow



34--34--------88



So how can I allow only one hyphen between the number?










share|improve this question




























    3















    I'm trying to match number with regular expression like:



    34-7878-3523-4233


    with this:



    ^[0-9][0-9-]*-[0-9-]*[0-9]$


    But the expression also allow



    34--34--------88



    So how can I allow only one hyphen between the number?










    share|improve this question
























      3












      3








      3








      I'm trying to match number with regular expression like:



      34-7878-3523-4233


      with this:



      ^[0-9][0-9-]*-[0-9-]*[0-9]$


      But the expression also allow



      34--34--------88



      So how can I allow only one hyphen between the number?










      share|improve this question














      I'm trying to match number with regular expression like:



      34-7878-3523-4233


      with this:



      ^[0-9][0-9-]*-[0-9-]*[0-9]$


      But the expression also allow



      34--34--------88



      So how can I allow only one hyphen between the number?







      regex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 25 '12 at 13:02









      vusanvusan

      3,7544 gold badges33 silver badges73 bronze badges




      3,7544 gold badges33 silver badges73 bronze badges






















          3 Answers
          3






          active

          oldest

          votes


















          17














          Your regex:



          See it in action: Regexr.com



          ^[0-9]+(-[0-9]+)+$


          Matches:



          1-2
          1-2-3


          Doesn't match:



          1
          1-
          1-2-
          1-2----3
          1---3





          share|improve this answer

























          • Thanks but for me there should exist at least one hyphen

            – vusan
            Dec 25 '12 at 13:09











          • @vusan OK, updated the regex.

            – mmdemirbas
            Dec 25 '12 at 13:10


















          6














          That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.



          You can try something like this: -



          ^([0-9]+-)*[0-9]+$


          Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.






          share|improve this answer























          • It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

            – vusan
            Dec 25 '12 at 13:07











          • @vusan. No it will match multiple digit now. I forgot to put quantifiers.

            – Rohit Jain
            Dec 25 '12 at 13:07


















          3














          Use the normal*(special normal*)* pattern:



          ^[0-9]+(-[0-9]+)+$


          where normal is [0-9] and special is -






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14030873%2fregex-number-and-hyphen%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            17














            Your regex:



            See it in action: Regexr.com



            ^[0-9]+(-[0-9]+)+$


            Matches:



            1-2
            1-2-3


            Doesn't match:



            1
            1-
            1-2-
            1-2----3
            1---3





            share|improve this answer

























            • Thanks but for me there should exist at least one hyphen

              – vusan
              Dec 25 '12 at 13:09











            • @vusan OK, updated the regex.

              – mmdemirbas
              Dec 25 '12 at 13:10















            17














            Your regex:



            See it in action: Regexr.com



            ^[0-9]+(-[0-9]+)+$


            Matches:



            1-2
            1-2-3


            Doesn't match:



            1
            1-
            1-2-
            1-2----3
            1---3





            share|improve this answer

























            • Thanks but for me there should exist at least one hyphen

              – vusan
              Dec 25 '12 at 13:09











            • @vusan OK, updated the regex.

              – mmdemirbas
              Dec 25 '12 at 13:10













            17












            17








            17







            Your regex:



            See it in action: Regexr.com



            ^[0-9]+(-[0-9]+)+$


            Matches:



            1-2
            1-2-3


            Doesn't match:



            1
            1-
            1-2-
            1-2----3
            1---3





            share|improve this answer















            Your regex:



            See it in action: Regexr.com



            ^[0-9]+(-[0-9]+)+$


            Matches:



            1-2
            1-2-3


            Doesn't match:



            1
            1-
            1-2-
            1-2----3
            1---3






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 25 '12 at 19:12

























            answered Dec 25 '12 at 13:05









            mmdemirbasmmdemirbas

            7,2162 gold badges34 silver badges50 bronze badges




            7,2162 gold badges34 silver badges50 bronze badges












            • Thanks but for me there should exist at least one hyphen

              – vusan
              Dec 25 '12 at 13:09











            • @vusan OK, updated the regex.

              – mmdemirbas
              Dec 25 '12 at 13:10

















            • Thanks but for me there should exist at least one hyphen

              – vusan
              Dec 25 '12 at 13:09











            • @vusan OK, updated the regex.

              – mmdemirbas
              Dec 25 '12 at 13:10
















            Thanks but for me there should exist at least one hyphen

            – vusan
            Dec 25 '12 at 13:09





            Thanks but for me there should exist at least one hyphen

            – vusan
            Dec 25 '12 at 13:09













            @vusan OK, updated the regex.

            – mmdemirbas
            Dec 25 '12 at 13:10





            @vusan OK, updated the regex.

            – mmdemirbas
            Dec 25 '12 at 13:10













            6














            That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.



            You can try something like this: -



            ^([0-9]+-)*[0-9]+$


            Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.






            share|improve this answer























            • It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

              – vusan
              Dec 25 '12 at 13:07











            • @vusan. No it will match multiple digit now. I forgot to put quantifiers.

              – Rohit Jain
              Dec 25 '12 at 13:07















            6














            That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.



            You can try something like this: -



            ^([0-9]+-)*[0-9]+$


            Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.






            share|improve this answer























            • It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

              – vusan
              Dec 25 '12 at 13:07











            • @vusan. No it will match multiple digit now. I forgot to put quantifiers.

              – Rohit Jain
              Dec 25 '12 at 13:07













            6












            6








            6







            That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.



            You can try something like this: -



            ^([0-9]+-)*[0-9]+$


            Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.






            share|improve this answer













            That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.



            You can try something like this: -



            ^([0-9]+-)*[0-9]+$


            Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 25 '12 at 13:04









            Rohit JainRohit Jain

            172k39 gold badges327 silver badges453 bronze badges




            172k39 gold badges327 silver badges453 bronze badges












            • It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

              – vusan
              Dec 25 '12 at 13:07











            • @vusan. No it will match multiple digit now. I forgot to put quantifiers.

              – Rohit Jain
              Dec 25 '12 at 13:07

















            • It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

              – vusan
              Dec 25 '12 at 13:07











            • @vusan. No it will match multiple digit now. I forgot to put quantifiers.

              – Rohit Jain
              Dec 25 '12 at 13:07
















            It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

            – vusan
            Dec 25 '12 at 13:07





            It just allow with one hyphen with single digit like 3-3-3. I want to match like 232424-42-2455

            – vusan
            Dec 25 '12 at 13:07













            @vusan. No it will match multiple digit now. I forgot to put quantifiers.

            – Rohit Jain
            Dec 25 '12 at 13:07





            @vusan. No it will match multiple digit now. I forgot to put quantifiers.

            – Rohit Jain
            Dec 25 '12 at 13:07











            3














            Use the normal*(special normal*)* pattern:



            ^[0-9]+(-[0-9]+)+$


            where normal is [0-9] and special is -






            share|improve this answer



























              3














              Use the normal*(special normal*)* pattern:



              ^[0-9]+(-[0-9]+)+$


              where normal is [0-9] and special is -






              share|improve this answer

























                3












                3








                3







                Use the normal*(special normal*)* pattern:



                ^[0-9]+(-[0-9]+)+$


                where normal is [0-9] and special is -






                share|improve this answer













                Use the normal*(special normal*)* pattern:



                ^[0-9]+(-[0-9]+)+$


                where normal is [0-9] and special is -







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 25 '12 at 13:05









                fgefge

                93k18 gold badges196 silver badges285 bronze badges




                93k18 gold badges196 silver badges285 bronze badges



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14030873%2fregex-number-and-hyphen%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript