Regexp struggling












1















I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:



d4rt
df5h
34d6
4567


But not 'erty'.



I get that pattern ([a-z]+|[0-9]+){4} but that keeps me the 4 letters case.










share|improve this question




















  • 1





    what is the language?

    – Ulugbek Umirov
    Nov 22 '18 at 18:06
















1















I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:



d4rt
df5h
34d6
4567


But not 'erty'.



I get that pattern ([a-z]+|[0-9]+){4} but that keeps me the 4 letters case.










share|improve this question




















  • 1





    what is the language?

    – Ulugbek Umirov
    Nov 22 '18 at 18:06














1












1








1








I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:



d4rt
df5h
34d6
4567


But not 'erty'.



I get that pattern ([a-z]+|[0-9]+){4} but that keeps me the 4 letters case.










share|improve this question
















I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:



d4rt
df5h
34d6
4567


But not 'erty'.



I get that pattern ([a-z]+|[0-9]+){4} but that keeps me the 4 letters case.







regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:57









Foo

1




1










asked Nov 22 '18 at 17:55









arnauddarnaudd

61




61








  • 1





    what is the language?

    – Ulugbek Umirov
    Nov 22 '18 at 18:06














  • 1





    what is the language?

    – Ulugbek Umirov
    Nov 22 '18 at 18:06








1




1





what is the language?

– Ulugbek Umirov
Nov 22 '18 at 18:06





what is the language?

– Ulugbek Umirov
Nov 22 '18 at 18:06












3 Answers
3






active

oldest

votes


















3














Your regex ([a-z]+|[0-9]+){4} uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.



If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.



To match a string with length of 4, you could use anchors to assert the start ^ and the end $ of the string.



^(?![a-z]{4})[a-z0-9]{4}$



Regex demo






share|improve this answer

































    1














    Your expression is matching four {4} of whatever either any number greater than 1 of lower case letters [a-z] or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.



    Your problem can be solved with lookaheads.



    (?=[a-z]{0,3}[0-9])[a-z0-9]{4}


    (?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".



    [a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.






    share|improve this answer





















    • 1





      In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

      – Tanktalus
      Nov 22 '18 at 18:24











    • Yup. Thanks for that! I fixed it with your suggestion.

      – ndvo
      Nov 22 '18 at 18:36





















    0














    As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,



    ^(?=.*d)[a-z0-9]{4}$


    Explanation:





    • ^ --> Start of input


    • (?=.*d) --> Look ahead to ensure the input contains at least one digit


    • [a-z0-9]{4} --> Ensures only lowercase alphabets and digits are matched in allowed character set


    • $ --> End of input


    Demo






    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%2f53436143%2fregexp-struggling%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









      3














      Your regex ([a-z]+|[0-9]+){4} uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.



      If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.



      To match a string with length of 4, you could use anchors to assert the start ^ and the end $ of the string.



      ^(?![a-z]{4})[a-z0-9]{4}$



      Regex demo






      share|improve this answer






























        3














        Your regex ([a-z]+|[0-9]+){4} uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.



        If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.



        To match a string with length of 4, you could use anchors to assert the start ^ and the end $ of the string.



        ^(?![a-z]{4})[a-z0-9]{4}$



        Regex demo






        share|improve this answer




























          3












          3








          3







          Your regex ([a-z]+|[0-9]+){4} uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.



          If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.



          To match a string with length of 4, you could use anchors to assert the start ^ and the end $ of the string.



          ^(?![a-z]{4})[a-z0-9]{4}$



          Regex demo






          share|improve this answer















          Your regex ([a-z]+|[0-9]+){4} uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.



          If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.



          To match a string with length of 4, you could use anchors to assert the start ^ and the end $ of the string.



          ^(?![a-z]{4})[a-z0-9]{4}$



          Regex demo







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 18:20

























          answered Nov 22 '18 at 18:09









          The fourth birdThe fourth bird

          21.7k81427




          21.7k81427

























              1














              Your expression is matching four {4} of whatever either any number greater than 1 of lower case letters [a-z] or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.



              Your problem can be solved with lookaheads.



              (?=[a-z]{0,3}[0-9])[a-z0-9]{4}


              (?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".



              [a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.






              share|improve this answer





















              • 1





                In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

                – Tanktalus
                Nov 22 '18 at 18:24











              • Yup. Thanks for that! I fixed it with your suggestion.

                – ndvo
                Nov 22 '18 at 18:36


















              1














              Your expression is matching four {4} of whatever either any number greater than 1 of lower case letters [a-z] or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.



              Your problem can be solved with lookaheads.



              (?=[a-z]{0,3}[0-9])[a-z0-9]{4}


              (?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".



              [a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.






              share|improve this answer





















              • 1





                In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

                – Tanktalus
                Nov 22 '18 at 18:24











              • Yup. Thanks for that! I fixed it with your suggestion.

                – ndvo
                Nov 22 '18 at 18:36
















              1












              1








              1







              Your expression is matching four {4} of whatever either any number greater than 1 of lower case letters [a-z] or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.



              Your problem can be solved with lookaheads.



              (?=[a-z]{0,3}[0-9])[a-z0-9]{4}


              (?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".



              [a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.






              share|improve this answer















              Your expression is matching four {4} of whatever either any number greater than 1 of lower case letters [a-z] or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.



              Your problem can be solved with lookaheads.



              (?=[a-z]{0,3}[0-9])[a-z0-9]{4}


              (?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".



              [a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 18:37

























              answered Nov 22 '18 at 18:11









              ndvondvo

              379110




              379110








              • 1





                In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

                – Tanktalus
                Nov 22 '18 at 18:24











              • Yup. Thanks for that! I fixed it with your suggestion.

                – ndvo
                Nov 22 '18 at 18:36
















              • 1





                In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

                – Tanktalus
                Nov 22 '18 at 18:24











              • Yup. Thanks for that! I fixed it with your suggestion.

                – ndvo
                Nov 22 '18 at 18:36










              1




              1





              In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

              – Tanktalus
              Nov 22 '18 at 18:24





              In case the string is more than four characters, e.g. erty7, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9]).

              – Tanktalus
              Nov 22 '18 at 18:24













              Yup. Thanks for that! I fixed it with your suggestion.

              – ndvo
              Nov 22 '18 at 18:36







              Yup. Thanks for that! I fixed it with your suggestion.

              – ndvo
              Nov 22 '18 at 18:36













              0














              As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,



              ^(?=.*d)[a-z0-9]{4}$


              Explanation:





              • ^ --> Start of input


              • (?=.*d) --> Look ahead to ensure the input contains at least one digit


              • [a-z0-9]{4} --> Ensures only lowercase alphabets and digits are matched in allowed character set


              • $ --> End of input


              Demo






              share|improve this answer




























                0














                As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,



                ^(?=.*d)[a-z0-9]{4}$


                Explanation:





                • ^ --> Start of input


                • (?=.*d) --> Look ahead to ensure the input contains at least one digit


                • [a-z0-9]{4} --> Ensures only lowercase alphabets and digits are matched in allowed character set


                • $ --> End of input


                Demo






                share|improve this answer


























                  0












                  0








                  0







                  As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,



                  ^(?=.*d)[a-z0-9]{4}$


                  Explanation:





                  • ^ --> Start of input


                  • (?=.*d) --> Look ahead to ensure the input contains at least one digit


                  • [a-z0-9]{4} --> Ensures only lowercase alphabets and digits are matched in allowed character set


                  • $ --> End of input


                  Demo






                  share|improve this answer













                  As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,



                  ^(?=.*d)[a-z0-9]{4}$


                  Explanation:





                  • ^ --> Start of input


                  • (?=.*d) --> Look ahead to ensure the input contains at least one digit


                  • [a-z0-9]{4} --> Ensures only lowercase alphabets and digits are matched in allowed character set


                  • $ --> End of input


                  Demo







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 20:03









                  Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

                  6,2132827




                  6,2132827






























                      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%2f53436143%2fregexp-struggling%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python