Regex: Detect Phone numbers that are separated by dashes (-) and/or spaces












1















I am trying to recognize these types of phone number inputs:




  1. 0172665476

  2. +6265476393

  3. +62-65476393

  4. +62-654-76393

  5. +62 65476393


While my regex: (?:d+s*)+ can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.



How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?










share|improve this question























  • @Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"

    – Dinuka
    Jan 2 at 16:40











  • check this regex101.com/r/wxIiPs/2

    – Code Maniac
    Jan 2 at 16:44
















1















I am trying to recognize these types of phone number inputs:




  1. 0172665476

  2. +6265476393

  3. +62-65476393

  4. +62-654-76393

  5. +62 65476393


While my regex: (?:d+s*)+ can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.



How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?










share|improve this question























  • @Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"

    – Dinuka
    Jan 2 at 16:40











  • check this regex101.com/r/wxIiPs/2

    – Code Maniac
    Jan 2 at 16:44














1












1








1








I am trying to recognize these types of phone number inputs:




  1. 0172665476

  2. +6265476393

  3. +62-65476393

  4. +62-654-76393

  5. +62 65476393


While my regex: (?:d+s*)+ can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.



How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?










share|improve this question














I am trying to recognize these types of phone number inputs:




  1. 0172665476

  2. +6265476393

  3. +62-65476393

  4. +62-654-76393

  5. +62 65476393


While my regex: (?:d+s*)+ can recognize the 1st 2 sample values, it recognizes the last 3 sample values as multiple matches in each line, instead of recognizing the number as a whole.



How can I modify this to support multiple dashes and/or spaces and still recognize it as 1 whole number instead of multiple matches?







regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 16:31









DinukaDinuka

1,88833580




1,88833580













  • @Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"

    – Dinuka
    Jan 2 at 16:40











  • check this regex101.com/r/wxIiPs/2

    – Code Maniac
    Jan 2 at 16:44



















  • @Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"

    – Dinuka
    Jan 2 at 16:40











  • check this regex101.com/r/wxIiPs/2

    – Code Maniac
    Jan 2 at 16:44

















@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"

– Dinuka
Jan 2 at 16:40





@Delgan Close! it includes a trailing space if there's any space after the number: "Hi my number is 0172665476 okay?"

– Dinuka
Jan 2 at 16:40













check this regex101.com/r/wxIiPs/2

– Code Maniac
Jan 2 at 16:44





check this regex101.com/r/wxIiPs/2

– Code Maniac
Jan 2 at 16:44












4 Answers
4






active

oldest

votes


















4














You may use this regex:



^+?d+(?:[s-]d+)*b


RegEx Details:





  • ^+?: Match optional + at start


  • d+: match 1+ digits


  • (?:[s-]d+)*: Match 0 or more groups that start with whitespace or - followed by 1+ digits


  • $: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)






share|improve this answer

































    1














    This should work:



    (?:[d +-]+)+





    share|improve this answer



















    • 1





      This matches all spaces as well: regex101.com/r/IoWOhp/2

      – Dinuka
      Jan 2 at 16:38



















    1














    This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)



    Regex: '^(?:[d +-]+)b'






    share|improve this answer

































      1














      Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +, and optional space or hyphen:



      (?:d{10}|+d{2}[- ]?d{3}-?d{5})b


      That will match:





      • (?: Non capturing group



        • d{10} Match 10 digits


        • | Or


        • +d{2}[-s]?d{3}-?d{5} Match +, 2 digits, optional whitespace char or -, 3 digits, optional -, 5 digits




      • )b Close non capturing group and word boundary


      Regex demo



      If your language supports negative lookbehinds you could prepend (?<!S) which checks that what comes before is not a non-whitespace character.






      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%2f54009879%2fregex-detect-phone-numbers-that-are-separated-by-dashes-and-or-spaces%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        You may use this regex:



        ^+?d+(?:[s-]d+)*b


        RegEx Details:





        • ^+?: Match optional + at start


        • d+: match 1+ digits


        • (?:[s-]d+)*: Match 0 or more groups that start with whitespace or - followed by 1+ digits


        • $: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)






        share|improve this answer






























          4














          You may use this regex:



          ^+?d+(?:[s-]d+)*b


          RegEx Details:





          • ^+?: Match optional + at start


          • d+: match 1+ digits


          • (?:[s-]d+)*: Match 0 or more groups that start with whitespace or - followed by 1+ digits


          • $: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)






          share|improve this answer




























            4












            4








            4







            You may use this regex:



            ^+?d+(?:[s-]d+)*b


            RegEx Details:





            • ^+?: Match optional + at start


            • d+: match 1+ digits


            • (?:[s-]d+)*: Match 0 or more groups that start with whitespace or - followed by 1+ digits


            • $: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)






            share|improve this answer















            You may use this regex:



            ^+?d+(?:[s-]d+)*b


            RegEx Details:





            • ^+?: Match optional + at start


            • d+: match 1+ digits


            • (?:[s-]d+)*: Match 0 or more groups that start with whitespace or - followed by 1+ digits


            • $: End (Replaced by word boundary as if there are trailing spaces, that match would be missed.)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 3 at 15:07









            Deep

            193211




            193211










            answered Jan 2 at 16:51









            anubhavaanubhava

            533k48331408




            533k48331408

























                1














                This should work:



                (?:[d +-]+)+





                share|improve this answer



















                • 1





                  This matches all spaces as well: regex101.com/r/IoWOhp/2

                  – Dinuka
                  Jan 2 at 16:38
















                1














                This should work:



                (?:[d +-]+)+





                share|improve this answer



















                • 1





                  This matches all spaces as well: regex101.com/r/IoWOhp/2

                  – Dinuka
                  Jan 2 at 16:38














                1












                1








                1







                This should work:



                (?:[d +-]+)+





                share|improve this answer













                This should work:



                (?:[d +-]+)+






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 16:36









                feliksfeliks

                1,070316




                1,070316








                • 1





                  This matches all spaces as well: regex101.com/r/IoWOhp/2

                  – Dinuka
                  Jan 2 at 16:38














                • 1





                  This matches all spaces as well: regex101.com/r/IoWOhp/2

                  – Dinuka
                  Jan 2 at 16:38








                1




                1





                This matches all spaces as well: regex101.com/r/IoWOhp/2

                – Dinuka
                Jan 2 at 16:38





                This matches all spaces as well: regex101.com/r/IoWOhp/2

                – Dinuka
                Jan 2 at 16:38











                1














                This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)



                Regex: '^(?:[d +-]+)b'






                share|improve this answer






























                  1














                  This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)



                  Regex: '^(?:[d +-]+)b'






                  share|improve this answer




























                    1












                    1








                    1







                    This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)



                    Regex: '^(?:[d +-]+)b'






                    share|improve this answer















                    This would work as per your reqt: (If there are trailing spaces, this regex will ignore.)



                    Regex: '^(?:[d +-]+)b'







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 2 at 18:11

























                    answered Jan 2 at 17:08









                    DeepDeep

                    193211




                    193211























                        1














                        Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +, and optional space or hyphen:



                        (?:d{10}|+d{2}[- ]?d{3}-?d{5})b


                        That will match:





                        • (?: Non capturing group



                          • d{10} Match 10 digits


                          • | Or


                          • +d{2}[-s]?d{3}-?d{5} Match +, 2 digits, optional whitespace char or -, 3 digits, optional -, 5 digits




                        • )b Close non capturing group and word boundary


                        Regex demo



                        If your language supports negative lookbehinds you could prepend (?<!S) which checks that what comes before is not a non-whitespace character.






                        share|improve this answer






























                          1














                          Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +, and optional space or hyphen:



                          (?:d{10}|+d{2}[- ]?d{3}-?d{5})b


                          That will match:





                          • (?: Non capturing group



                            • d{10} Match 10 digits


                            • | Or


                            • +d{2}[-s]?d{3}-?d{5} Match +, 2 digits, optional whitespace char or -, 3 digits, optional -, 5 digits




                          • )b Close non capturing group and word boundary


                          Regex demo



                          If your language supports negative lookbehinds you could prepend (?<!S) which checks that what comes before is not a non-whitespace character.






                          share|improve this answer




























                            1












                            1








                            1







                            Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +, and optional space or hyphen:



                            (?:d{10}|+d{2}[- ]?d{3}-?d{5})b


                            That will match:





                            • (?: Non capturing group



                              • d{10} Match 10 digits


                              • | Or


                              • +d{2}[-s]?d{3}-?d{5} Match +, 2 digits, optional whitespace char or -, 3 digits, optional -, 5 digits




                            • )b Close non capturing group and word boundary


                            Regex demo



                            If your language supports negative lookbehinds you could prepend (?<!S) which checks that what comes before is not a non-whitespace character.






                            share|improve this answer















                            Another option could be to use an alternation to match either 10 digits without a leading plus sign or match the pattern with a +, and optional space or hyphen:



                            (?:d{10}|+d{2}[- ]?d{3}-?d{5})b


                            That will match:





                            • (?: Non capturing group



                              • d{10} Match 10 digits


                              • | Or


                              • +d{2}[-s]?d{3}-?d{5} Match +, 2 digits, optional whitespace char or -, 3 digits, optional -, 5 digits




                            • )b Close non capturing group and word boundary


                            Regex demo



                            If your language supports negative lookbehinds you could prepend (?<!S) which checks that what comes before is not a non-whitespace character.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 2 at 19:51

























                            answered Jan 2 at 18:37









                            The fourth birdThe fourth bird

                            24.5k81629




                            24.5k81629






























                                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%2f54009879%2fregex-detect-phone-numbers-that-are-separated-by-dashes-and-or-spaces%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

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory