Need regex for a string having characters followed by even number of digits












-1














Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?



Valid:



a11a1121
bbbb11a1121


Invalid:



a11a1


I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.



Can you please help in this regard?










share|improve this question




















  • 2




    Possible duplicate of Learning Regular Expressions
    – Biffen
    Nov 12 at 11:52
















-1














Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?



Valid:



a11a1121
bbbb11a1121


Invalid:



a11a1


I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.



Can you please help in this regard?










share|improve this question




















  • 2




    Possible duplicate of Learning Regular Expressions
    – Biffen
    Nov 12 at 11:52














-1












-1








-1







Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?



Valid:



a11a1121
bbbb11a1121


Invalid:



a11a1


I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.



Can you please help in this regard?










share|improve this question















Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?



Valid:



a11a1121
bbbb11a1121


Invalid:



a11a1


I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.



Can you please help in this regard?







java regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 11:53









Biffen

4,16352129




4,16352129










asked Nov 12 at 11:47









Icche Guri

96211130




96211130








  • 2




    Possible duplicate of Learning Regular Expressions
    – Biffen
    Nov 12 at 11:52














  • 2




    Possible duplicate of Learning Regular Expressions
    – Biffen
    Nov 12 at 11:52








2




2




Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52




Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52












2 Answers
2






active

oldest

votes


















1














The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]



You can break down your requirement to groups and then handle it accordingly.



Like you require at least one character. so you start with ^([a-zA-Z]+)$



Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$



Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$



You can use online tools like regex101 for these purpose. The provided regex in action here






share|improve this answer





























    1














    You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$



    Explanation :





    • [a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one


    • ([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.






    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%2f53261522%2fneed-regex-for-a-string-having-characters-followed-by-even-number-of-digits%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









      1














      The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]



      You can break down your requirement to groups and then handle it accordingly.



      Like you require at least one character. so you start with ^([a-zA-Z]+)$



      Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$



      Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$



      You can use online tools like regex101 for these purpose. The provided regex in action here






      share|improve this answer


























        1














        The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]



        You can break down your requirement to groups and then handle it accordingly.



        Like you require at least one character. so you start with ^([a-zA-Z]+)$



        Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$



        Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$



        You can use online tools like regex101 for these purpose. The provided regex in action here






        share|improve this answer
























          1












          1








          1






          The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]



          You can break down your requirement to groups and then handle it accordingly.



          Like you require at least one character. so you start with ^([a-zA-Z]+)$



          Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$



          Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$



          You can use online tools like regex101 for these purpose. The provided regex in action here






          share|improve this answer












          The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]



          You can break down your requirement to groups and then handle it accordingly.



          Like you require at least one character. so you start with ^([a-zA-Z]+)$



          Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$



          Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$



          You can use online tools like regex101 for these purpose. The provided regex in action here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 12:01









          suvartheec

          1,121917




          1,121917

























              1














              You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$



              Explanation :





              • [a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one


              • ([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.






              share|improve this answer




























                1














                You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$



                Explanation :





                • [a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one


                • ([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.






                share|improve this answer


























                  1












                  1








                  1






                  You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$



                  Explanation :





                  • [a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one


                  • ([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.






                  share|improve this answer














                  You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$



                  Explanation :





                  • [a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one


                  • ([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 12:29

























                  answered Nov 12 at 11:54









                  Amessihel

                  1,9631723




                  1,9631723






























                      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%2f53261522%2fneed-regex-for-a-string-having-characters-followed-by-even-number-of-digits%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python