Regex - matching brazilian phone numbers












2















I'm trying to use Regex to validate brazilian phone numbers.



For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.



RegEX:



(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}


Valid numbers:



(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true


Invalid numbers:



23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true


Thanks!










share|improve this question

























  • If you match the whole string by enclosing the whole regex with ^ and $, will it be OK?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:15











  • Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.

    – Havenard
    Nov 21 '18 at 17:16











  • @Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.

    – Zeca Novaes
    Nov 21 '18 at 17:19











  • @WiktorStribiżew Not working :(

    – Zeca Novaes
    Nov 21 '18 at 17:19






  • 1





    Try (?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b. See demo.

    – Wiktor Stribiżew
    Nov 21 '18 at 17:21


















2















I'm trying to use Regex to validate brazilian phone numbers.



For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.



RegEX:



(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}


Valid numbers:



(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true


Invalid numbers:



23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true


Thanks!










share|improve this question

























  • If you match the whole string by enclosing the whole regex with ^ and $, will it be OK?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:15











  • Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.

    – Havenard
    Nov 21 '18 at 17:16











  • @Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.

    – Zeca Novaes
    Nov 21 '18 at 17:19











  • @WiktorStribiżew Not working :(

    – Zeca Novaes
    Nov 21 '18 at 17:19






  • 1





    Try (?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b. See demo.

    – Wiktor Stribiżew
    Nov 21 '18 at 17:21
















2












2








2








I'm trying to use Regex to validate brazilian phone numbers.



For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.



RegEX:



(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}


Valid numbers:



(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true


Invalid numbers:



23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true


Thanks!










share|improve this question
















I'm trying to use Regex to validate brazilian phone numbers.



For some reason, I cannot use b to indicate the beginning and the end for the match. In this case, my Regex selects some invalid phone numbers as valid.



RegEX:



(?([0-9]{2,3}|0{1}((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}


Valid numbers:



(00)0000-0000; true
(00)000000000; true
(00) 00000 0000; true
00 00000 0000; true
(000)00000 0000; true


Invalid numbers:



23232443243243423432; true
(0000)000000000; true
a00 00000-0000 ; true


Thanks!







javascript regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:12









Poul Bak

5,45831232




5,45831232










asked Nov 21 '18 at 17:12









Zeca NovaesZeca Novaes

14510




14510













  • If you match the whole string by enclosing the whole regex with ^ and $, will it be OK?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:15











  • Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.

    – Havenard
    Nov 21 '18 at 17:16











  • @Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.

    – Zeca Novaes
    Nov 21 '18 at 17:19











  • @WiktorStribiżew Not working :(

    – Zeca Novaes
    Nov 21 '18 at 17:19






  • 1





    Try (?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b. See demo.

    – Wiktor Stribiżew
    Nov 21 '18 at 17:21





















  • If you match the whole string by enclosing the whole regex with ^ and $, will it be OK?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:15











  • Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.

    – Havenard
    Nov 21 '18 at 17:16











  • @Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.

    – Zeca Novaes
    Nov 21 '18 at 17:19











  • @WiktorStribiżew Not working :(

    – Zeca Novaes
    Nov 21 '18 at 17:19






  • 1





    Try (?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b. See demo.

    – Wiktor Stribiżew
    Nov 21 '18 at 17:21



















If you match the whole string by enclosing the whole regex with ^ and $, will it be OK?

– Wiktor Stribiżew
Nov 21 '18 at 17:15





If you match the whole string by enclosing the whole regex with ^ and $, will it be OK?

– Wiktor Stribiżew
Nov 21 '18 at 17:15













Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.

– Havenard
Nov 21 '18 at 17:16





Parentheses, spaces and dashes don't actually mean anything as far as telephone devices are concerned, you could very well strip those from the string before validating.

– Havenard
Nov 21 '18 at 17:16













@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.

– Zeca Novaes
Nov 21 '18 at 17:19





@Havenard, I'm using this Regex as Watson Assistant pattern. That's the problem.

– Zeca Novaes
Nov 21 '18 at 17:19













@WiktorStribiżew Not working :(

– Zeca Novaes
Nov 21 '18 at 17:19





@WiktorStribiżew Not working :(

– Zeca Novaes
Nov 21 '18 at 17:19




1




1





Try (?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b. See demo.

– Wiktor Stribiżew
Nov 21 '18 at 17:21







Try (?(b[0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b. See demo.

– Wiktor Stribiżew
Nov 21 '18 at 17:21














1 Answer
1






active

oldest

votes


















3














You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.



(?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
^^ ^^


See the regex demo



If you put b before (?, when there is a ( before a digit, the word boundary will invalidate the match if there is no word char right before the (.






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%2f53417334%2fregex-matching-brazilian-phone-numbers%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.



    (?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
    ^^ ^^


    See the regex demo



    If you put b before (?, when there is a ( before a digit, the word boundary will invalidate the match if there is no word char right before the (.






    share|improve this answer




























      3














      You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.



      (?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
      ^^ ^^


      See the regex demo



      If you put b before (?, when there is a ( before a digit, the word boundary will invalidate the match if there is no word char right before the (.






      share|improve this answer


























        3












        3








        3







        You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.



        (?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
        ^^ ^^


        See the regex demo



        If you put b before (?, when there is a ( before a digit, the word boundary will invalidate the match if there is no word char right before the (.






        share|improve this answer













        You should place word boundaries at the "right" places. They must placed at the first obligatory word char matching pattern and after the last obligatory word char.



        (?b([0-9]{2,3}|0((x|[0-9]){2,3}[0-9]{2})))?s*[0-9]{4,5}[- ]*[0-9]{4}b
        ^^ ^^


        See the regex demo



        If you put b before (?, when there is a ( before a digit, the word boundary will invalidate the match if there is no word char right before the (.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 17:25









        Wiktor StribiżewWiktor Stribiżew

        311k16132207




        311k16132207






























            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%2f53417334%2fregex-matching-brazilian-phone-numbers%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin