android Regex issue












2














I had an issue with this regex:



({(([^p{Space}][^p{Punct}])+)})


The problem is in number of chars. If I typing even number of chars it's works, when odd - not. I was trying to replace '+' with '?' or '*', but result still the same. How can I fix this?



I expect from this regex to block such strings: {%,$ #fd}. And allow this:
{F} or {F242fFSf23}.










share|improve this question
























  • What are the pattern requirements? What are you trying to match? Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:08












  • When you are trying to replace + then how even or odd numbers are coming into the picture?
    – Ümañg ßürmån
    Nov 8 '18 at 9:09










  • @WiktorStribiżew I'm trying to block user from entering such string: {%F ,2}. And allow only to type: {Ffrgr2443fdfd}
    – Skullper
    Nov 8 '18 at 9:09










  • Try {[^p{Punct}p{Space}]+} or {[^p{P}p{S}s]+} or even {p{Alnum}+} or {[A-Za-z0-9]+}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:11












  • @WiktorStribiżew You're awesome. First and second regex working. Thank you!
    – Skullper
    Nov 8 '18 at 9:14


















2














I had an issue with this regex:



({(([^p{Space}][^p{Punct}])+)})


The problem is in number of chars. If I typing even number of chars it's works, when odd - not. I was trying to replace '+' with '?' or '*', but result still the same. How can I fix this?



I expect from this regex to block such strings: {%,$ #fd}. And allow this:
{F} or {F242fFSf23}.










share|improve this question
























  • What are the pattern requirements? What are you trying to match? Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:08












  • When you are trying to replace + then how even or odd numbers are coming into the picture?
    – Ümañg ßürmån
    Nov 8 '18 at 9:09










  • @WiktorStribiżew I'm trying to block user from entering such string: {%F ,2}. And allow only to type: {Ffrgr2443fdfd}
    – Skullper
    Nov 8 '18 at 9:09










  • Try {[^p{Punct}p{Space}]+} or {[^p{P}p{S}s]+} or even {p{Alnum}+} or {[A-Za-z0-9]+}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:11












  • @WiktorStribiżew You're awesome. First and second regex working. Thank you!
    – Skullper
    Nov 8 '18 at 9:14
















2












2








2







I had an issue with this regex:



({(([^p{Space}][^p{Punct}])+)})


The problem is in number of chars. If I typing even number of chars it's works, when odd - not. I was trying to replace '+' with '?' or '*', but result still the same. How can I fix this?



I expect from this regex to block such strings: {%,$ #fd}. And allow this:
{F} or {F242fFSf23}.










share|improve this question















I had an issue with this regex:



({(([^p{Space}][^p{Punct}])+)})


The problem is in number of chars. If I typing even number of chars it's works, when odd - not. I was trying to replace '+' with '?' or '*', but result still the same. How can I fix this?



I expect from this regex to block such strings: {%,$ #fd}. And allow this:
{F} or {F242fFSf23}.







android regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 8:53









Jayson Minard

37.9k15107170




37.9k15107170










asked Nov 8 '18 at 9:05









Skullper

19111




19111












  • What are the pattern requirements? What are you trying to match? Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:08












  • When you are trying to replace + then how even or odd numbers are coming into the picture?
    – Ümañg ßürmån
    Nov 8 '18 at 9:09










  • @WiktorStribiżew I'm trying to block user from entering such string: {%F ,2}. And allow only to type: {Ffrgr2443fdfd}
    – Skullper
    Nov 8 '18 at 9:09










  • Try {[^p{Punct}p{Space}]+} or {[^p{P}p{S}s]+} or even {p{Alnum}+} or {[A-Za-z0-9]+}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:11












  • @WiktorStribiżew You're awesome. First and second regex working. Thank you!
    – Skullper
    Nov 8 '18 at 9:14




















  • What are the pattern requirements? What are you trying to match? Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:08












  • When you are trying to replace + then how even or odd numbers are coming into the picture?
    – Ümañg ßürmån
    Nov 8 '18 at 9:09










  • @WiktorStribiżew I'm trying to block user from entering such string: {%F ,2}. And allow only to type: {Ffrgr2443fdfd}
    – Skullper
    Nov 8 '18 at 9:09










  • Try {[^p{Punct}p{Space}]+} or {[^p{P}p{S}s]+} or even {p{Alnum}+} or {[A-Za-z0-9]+}
    – Wiktor Stribiżew
    Nov 8 '18 at 9:11












  • @WiktorStribiżew You're awesome. First and second regex working. Thank you!
    – Skullper
    Nov 8 '18 at 9:14


















What are the pattern requirements? What are you trying to match? Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}
– Wiktor Stribiżew
Nov 8 '18 at 9:08






What are the pattern requirements? What are you trying to match? Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}
– Wiktor Stribiżew
Nov 8 '18 at 9:08














When you are trying to replace + then how even or odd numbers are coming into the picture?
– Ümañg ßürmån
Nov 8 '18 at 9:09




When you are trying to replace + then how even or odd numbers are coming into the picture?
– Ümañg ßürmån
Nov 8 '18 at 9:09












@WiktorStribiżew I'm trying to block user from entering such string: {%F ,2}. And allow only to type: {Ffrgr2443fdfd}
– Skullper
Nov 8 '18 at 9:09




@WiktorStribiżew I'm trying to block user from entering such string: {%F ,2}. And allow only to type: {Ffrgr2443fdfd}
– Skullper
Nov 8 '18 at 9:09












Try {[^p{Punct}p{Space}]+} or {[^p{P}p{S}s]+} or even {p{Alnum}+} or {[A-Za-z0-9]+}
– Wiktor Stribiżew
Nov 8 '18 at 9:11






Try {[^p{Punct}p{Space}]+} or {[^p{P}p{S}s]+} or even {p{Alnum}+} or {[A-Za-z0-9]+}
– Wiktor Stribiżew
Nov 8 '18 at 9:11














@WiktorStribiżew You're awesome. First and second regex working. Thank you!
– Skullper
Nov 8 '18 at 9:14






@WiktorStribiżew You're awesome. First and second regex working. Thank you!
– Skullper
Nov 8 '18 at 9:14














2 Answers
2






active

oldest

votes


















2














Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.



To fix that, you need to use both the character classes inside bracket expression:



{[^p{Punct}p{Space}]+} 


or



{[^p{P}p{S}s]+}


Details





  • { - a { char


  • [^p{Punct}p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the p{Punct} (punctuation) or p{Space} (whitespace) class.


  • } - a }.


Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere



{[A-Za-z0-9]+}





share|improve this answer

















  • 1




    yep, the last regex works too
    – Skullper
    Nov 8 '18 at 9:18



















0














Disassembling your regex... the reason why it only accepts an even number in between is the following part:



([^p{Space}][^p{Punct}])+


This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.



So what you probably rather want is the following:



[^p{Space}p{Punct}]+


for the part shown above... which will result in the following for your complete regex:



{[^p{Space}p{Punct}]+}


that of course can be simplified even more. I leave that up to you.






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%2f53204474%2fandroid-regex-issue%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.



    To fix that, you need to use both the character classes inside bracket expression:



    {[^p{Punct}p{Space}]+} 


    or



    {[^p{P}p{S}s]+}


    Details





    • { - a { char


    • [^p{Punct}p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the p{Punct} (punctuation) or p{Space} (whitespace) class.


    • } - a }.


    Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere



    {[A-Za-z0-9]+}





    share|improve this answer

















    • 1




      yep, the last regex works too
      – Skullper
      Nov 8 '18 at 9:18
















    2














    Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.



    To fix that, you need to use both the character classes inside bracket expression:



    {[^p{Punct}p{Space}]+} 


    or



    {[^p{P}p{S}s]+}


    Details





    • { - a { char


    • [^p{Punct}p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the p{Punct} (punctuation) or p{Space} (whitespace) class.


    • } - a }.


    Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere



    {[A-Za-z0-9]+}





    share|improve this answer

















    • 1




      yep, the last regex works too
      – Skullper
      Nov 8 '18 at 9:18














    2












    2








    2






    Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.



    To fix that, you need to use both the character classes inside bracket expression:



    {[^p{Punct}p{Space}]+} 


    or



    {[^p{P}p{S}s]+}


    Details





    • { - a { char


    • [^p{Punct}p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the p{Punct} (punctuation) or p{Space} (whitespace) class.


    • } - a }.


    Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere



    {[A-Za-z0-9]+}





    share|improve this answer












    Currently, it matches a {, then 1 or more repetitions of 2 chars, a non-space and then a non-punctuation, and then a }, hence you cannot use 1 char in between {...}.



    To fix that, you need to use both the character classes inside bracket expression:



    {[^p{Punct}p{Space}]+} 


    or



    {[^p{P}p{S}s]+}


    Details





    • { - a { char


    • [^p{Punct}p{Space}]+ - 1 or more repetitons (+) of any char that does not belong to the p{Punct} (punctuation) or p{Space} (whitespace) class.


    • } - a }.


    Note that if the contents between the braces can only include ASCII letters or digits (in regex, [A-Za-z0-9]+), you may even use a mere



    {[A-Za-z0-9]+}






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 '18 at 9:15









    Wiktor Stribiżew

    309k16126202




    309k16126202








    • 1




      yep, the last regex works too
      – Skullper
      Nov 8 '18 at 9:18














    • 1




      yep, the last regex works too
      – Skullper
      Nov 8 '18 at 9:18








    1




    1




    yep, the last regex works too
    – Skullper
    Nov 8 '18 at 9:18




    yep, the last regex works too
    – Skullper
    Nov 8 '18 at 9:18













    0














    Disassembling your regex... the reason why it only accepts an even number in between is the following part:



    ([^p{Space}][^p{Punct}])+


    This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.



    So what you probably rather want is the following:



    [^p{Space}p{Punct}]+


    for the part shown above... which will result in the following for your complete regex:



    {[^p{Space}p{Punct}]+}


    that of course can be simplified even more. I leave that up to you.






    share|improve this answer


























      0














      Disassembling your regex... the reason why it only accepts an even number in between is the following part:



      ([^p{Space}][^p{Punct}])+


      This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.



      So what you probably rather want is the following:



      [^p{Space}p{Punct}]+


      for the part shown above... which will result in the following for your complete regex:



      {[^p{Space}p{Punct}]+}


      that of course can be simplified even more. I leave that up to you.






      share|improve this answer
























        0












        0








        0






        Disassembling your regex... the reason why it only accepts an even number in between is the following part:



        ([^p{Space}][^p{Punct}])+


        This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.



        So what you probably rather want is the following:



        [^p{Space}p{Punct}]+


        for the part shown above... which will result in the following for your complete regex:



        {[^p{Space}p{Punct}]+}


        that of course can be simplified even more. I leave that up to you.






        share|improve this answer












        Disassembling your regex... the reason why it only accepts an even number in between is the following part:



        ([^p{Space}][^p{Punct}])+


        This basically means: something which isn't a space, exactly 1 character and something which isn't a ~punct, exactly 1 character and this several times... so exactly 1 + exactly another 1 are exactly 2 characters... and this several times will always be even.



        So what you probably rather want is the following:



        [^p{Space}p{Punct}]+


        for the part shown above... which will result in the following for your complete regex:



        {[^p{Space}p{Punct}]+}


        that of course can be simplified even more. I leave that up to you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 '18 at 9:14









        Roland

        9,43711141




        9,43711141






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53204474%2fandroid-regex-issue%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

            Full-time equivalent

            Bicuculline

            さくらももこ