regex - any number of digits + digit or [a-z]












1















I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.



So:





  • 29c is fine


  • 29 is fine


  • 2425315651252fsaw fails


  • 24241jl.421c fails


  • c fails


The regex I have so far is (^d+)([a-z]{1}|d) which passes the 29, 20c, but also passes stuff like 29cdsd.



What am I doing wrong?










share|improve this question

























  • Please let know if a and 1 are valid inputs.

    – Wiktor Stribiżew
    Nov 23 '18 at 13:05


















1















I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.



So:





  • 29c is fine


  • 29 is fine


  • 2425315651252fsaw fails


  • 24241jl.421c fails


  • c fails


The regex I have so far is (^d+)([a-z]{1}|d) which passes the 29, 20c, but also passes stuff like 29cdsd.



What am I doing wrong?










share|improve this question

























  • Please let know if a and 1 are valid inputs.

    – Wiktor Stribiżew
    Nov 23 '18 at 13:05
















1












1








1








I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.



So:





  • 29c is fine


  • 29 is fine


  • 2425315651252fsaw fails


  • 24241jl.421c fails


  • c fails


The regex I have so far is (^d+)([a-z]{1}|d) which passes the 29, 20c, but also passes stuff like 29cdsd.



What am I doing wrong?










share|improve this question
















I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.



So:





  • 29c is fine


  • 29 is fine


  • 2425315651252fsaw fails


  • 24241jl.421c fails


  • c fails


The regex I have so far is (^d+)([a-z]{1}|d) which passes the 29, 20c, but also passes stuff like 29cdsd.



What am I doing wrong?







regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:15







Miha Šušteršič

















asked Nov 23 '18 at 12:55









Miha ŠušteršičMiha Šušteršič

3,711104479




3,711104479













  • Please let know if a and 1 are valid inputs.

    – Wiktor Stribiżew
    Nov 23 '18 at 13:05





















  • Please let know if a and 1 are valid inputs.

    – Wiktor Stribiżew
    Nov 23 '18 at 13:05



















Please let know if a and 1 are valid inputs.

– Wiktor Stribiżew
Nov 23 '18 at 13:05







Please let know if a and 1 are valid inputs.

– Wiktor Stribiżew
Nov 23 '18 at 13:05














5 Answers
5






active

oldest

votes


















4














Your (^d+)([a-z]{1}|d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.



Use



^[0-9]+[a-z0-9]?$


See regex demo



Details





  • ^ - start of string


  • [0-9]+ - any 1 or more digits


  • [a-z0-9]? - 1 or 0 lowercase ASCII letters or digits


  • $ - end of string.






share|improve this answer





















  • 1





    Wouldn't this allow the string to be a single character a-z?

    – ohaal
    Nov 23 '18 at 12:59











  • @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

    – Wiktor Stribiżew
    Nov 23 '18 at 12:59













  • Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

    – ohaal
    Nov 23 '18 at 13:00











  • @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

    – Wiktor Stribiżew
    Nov 23 '18 at 13:03













  • Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

    – Miha Šušteršič
    Nov 23 '18 at 13:14





















2














This should follow your rules exactly.



^d+[a-z]?$






share|improve this answer































    0














    if "any number of digits" might be zero ^d*w$






    share|improve this answer































      0














      You could add an anchor $ to assert the end of the line and you can omit the {1} part:



      ^(d+)([a-z]|d)$



      In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:



      ^d+[a-zd]$



      Regex demo



      That would match:





      • ^ Assert the start of the string


      • d+ Match 1+ digits


      • [a-zd] A character class which matches a-z or a digit


      • $ Assert the end of the string






      share|improve this answer

































        -2














        ^ - start of string



        d* - any amount of digits, 0 or more



        [a-zA-z] - a lowercase and uppercase ASCII letters.



        $ - end of string






        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%2f53447133%2fregex-any-number-of-digits-digit-or-a-z%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Your (^d+)([a-z]{1}|d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.



          Use



          ^[0-9]+[a-z0-9]?$


          See regex demo



          Details





          • ^ - start of string


          • [0-9]+ - any 1 or more digits


          • [a-z0-9]? - 1 or 0 lowercase ASCII letters or digits


          • $ - end of string.






          share|improve this answer





















          • 1





            Wouldn't this allow the string to be a single character a-z?

            – ohaal
            Nov 23 '18 at 12:59











          • @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

            – Wiktor Stribiżew
            Nov 23 '18 at 12:59













          • Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

            – ohaal
            Nov 23 '18 at 13:00











          • @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

            – Wiktor Stribiżew
            Nov 23 '18 at 13:03













          • Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

            – Miha Šušteršič
            Nov 23 '18 at 13:14


















          4














          Your (^d+)([a-z]{1}|d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.



          Use



          ^[0-9]+[a-z0-9]?$


          See regex demo



          Details





          • ^ - start of string


          • [0-9]+ - any 1 or more digits


          • [a-z0-9]? - 1 or 0 lowercase ASCII letters or digits


          • $ - end of string.






          share|improve this answer





















          • 1





            Wouldn't this allow the string to be a single character a-z?

            – ohaal
            Nov 23 '18 at 12:59











          • @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

            – Wiktor Stribiżew
            Nov 23 '18 at 12:59













          • Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

            – ohaal
            Nov 23 '18 at 13:00











          • @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

            – Wiktor Stribiżew
            Nov 23 '18 at 13:03













          • Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

            – Miha Šušteršič
            Nov 23 '18 at 13:14
















          4












          4








          4







          Your (^d+)([a-z]{1}|d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.



          Use



          ^[0-9]+[a-z0-9]?$


          See regex demo



          Details





          • ^ - start of string


          • [0-9]+ - any 1 or more digits


          • [a-z0-9]? - 1 or 0 lowercase ASCII letters or digits


          • $ - end of string.






          share|improve this answer















          Your (^d+)([a-z]{1}|d) passes 29cdsd because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.



          Use



          ^[0-9]+[a-z0-9]?$


          See regex demo



          Details





          • ^ - start of string


          • [0-9]+ - any 1 or more digits


          • [a-z0-9]? - 1 or 0 lowercase ASCII letters or digits


          • $ - end of string.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 13:15

























          answered Nov 23 '18 at 12:56









          Wiktor StribiżewWiktor Stribiżew

          310k16131206




          310k16131206








          • 1





            Wouldn't this allow the string to be a single character a-z?

            – ohaal
            Nov 23 '18 at 12:59











          • @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

            – Wiktor Stribiżew
            Nov 23 '18 at 12:59













          • Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

            – ohaal
            Nov 23 '18 at 13:00











          • @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

            – Wiktor Stribiżew
            Nov 23 '18 at 13:03













          • Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

            – Miha Šušteršič
            Nov 23 '18 at 13:14
















          • 1





            Wouldn't this allow the string to be a single character a-z?

            – ohaal
            Nov 23 '18 at 12:59











          • @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

            – Wiktor Stribiżew
            Nov 23 '18 at 12:59













          • Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

            – ohaal
            Nov 23 '18 at 13:00











          • @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

            – Wiktor Stribiżew
            Nov 23 '18 at 13:03













          • Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

            – Miha Šušteršič
            Nov 23 '18 at 13:14










          1




          1





          Wouldn't this allow the string to be a single character a-z?

          – ohaal
          Nov 23 '18 at 12:59





          Wouldn't this allow the string to be a single character a-z?

          – ohaal
          Nov 23 '18 at 12:59













          @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

          – Wiktor Stribiżew
          Nov 23 '18 at 12:59







          @ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.

          – Wiktor Stribiżew
          Nov 23 '18 at 12:59















          Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

          – ohaal
          Nov 23 '18 at 13:00





          Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.

          – ohaal
          Nov 23 '18 at 13:00













          @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

          – Wiktor Stribiżew
          Nov 23 '18 at 13:03







          @ohaal Well, we need confirmation from OP. Your ^d+[a-z]?$ solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9] here because OP did not specify the regex flavor. Not all flavors support d.

          – Wiktor Stribiżew
          Nov 23 '18 at 13:03















          Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

          – Miha Šušteršič
          Nov 23 '18 at 13:14







          Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.

          – Miha Šušteršič
          Nov 23 '18 at 13:14















          2














          This should follow your rules exactly.



          ^d+[a-z]?$






          share|improve this answer




























            2














            This should follow your rules exactly.



            ^d+[a-z]?$






            share|improve this answer


























              2












              2








              2







              This should follow your rules exactly.



              ^d+[a-z]?$






              share|improve this answer













              This should follow your rules exactly.



              ^d+[a-z]?$







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '18 at 12:57









              ohaalohaal

              4,69222646




              4,69222646























                  0














                  if "any number of digits" might be zero ^d*w$






                  share|improve this answer




























                    0














                    if "any number of digits" might be zero ^d*w$






                    share|improve this answer


























                      0












                      0








                      0







                      if "any number of digits" might be zero ^d*w$






                      share|improve this answer













                      if "any number of digits" might be zero ^d*w$







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 23 '18 at 13:15









                      ekvalizerekvalizer

                      214




                      214























                          0














                          You could add an anchor $ to assert the end of the line and you can omit the {1} part:



                          ^(d+)([a-z]|d)$



                          In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:



                          ^d+[a-zd]$



                          Regex demo



                          That would match:





                          • ^ Assert the start of the string


                          • d+ Match 1+ digits


                          • [a-zd] A character class which matches a-z or a digit


                          • $ Assert the end of the string






                          share|improve this answer






























                            0














                            You could add an anchor $ to assert the end of the line and you can omit the {1} part:



                            ^(d+)([a-z]|d)$



                            In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:



                            ^d+[a-zd]$



                            Regex demo



                            That would match:





                            • ^ Assert the start of the string


                            • d+ Match 1+ digits


                            • [a-zd] A character class which matches a-z or a digit


                            • $ Assert the end of the string






                            share|improve this answer




























                              0












                              0








                              0







                              You could add an anchor $ to assert the end of the line and you can omit the {1} part:



                              ^(d+)([a-z]|d)$



                              In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:



                              ^d+[a-zd]$



                              Regex demo



                              That would match:





                              • ^ Assert the start of the string


                              • d+ Match 1+ digits


                              • [a-zd] A character class which matches a-z or a digit


                              • $ Assert the end of the string






                              share|improve this answer















                              You could add an anchor $ to assert the end of the line and you can omit the {1} part:



                              ^(d+)([a-z]|d)$



                              In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:



                              ^d+[a-zd]$



                              Regex demo



                              That would match:





                              • ^ Assert the start of the string


                              • d+ Match 1+ digits


                              • [a-zd] A character class which matches a-z or a digit


                              • $ Assert the end of the string







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 23 '18 at 13:27

























                              answered Nov 23 '18 at 13:13









                              The fourth birdThe fourth bird

                              21.1k71326




                              21.1k71326























                                  -2














                                  ^ - start of string



                                  d* - any amount of digits, 0 or more



                                  [a-zA-z] - a lowercase and uppercase ASCII letters.



                                  $ - end of string






                                  share|improve this answer






























                                    -2














                                    ^ - start of string



                                    d* - any amount of digits, 0 or more



                                    [a-zA-z] - a lowercase and uppercase ASCII letters.



                                    $ - end of string






                                    share|improve this answer




























                                      -2












                                      -2








                                      -2







                                      ^ - start of string



                                      d* - any amount of digits, 0 or more



                                      [a-zA-z] - a lowercase and uppercase ASCII letters.



                                      $ - end of string






                                      share|improve this answer















                                      ^ - start of string



                                      d* - any amount of digits, 0 or more



                                      [a-zA-z] - a lowercase and uppercase ASCII letters.



                                      $ - end of string







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 23 '18 at 13:21

























                                      answered Nov 23 '18 at 13:13









                                      GK DevloperGK Devloper

                                      12




                                      12






























                                          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%2f53447133%2fregex-any-number-of-digits-digit-or-a-z%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

                                          Berounka

                                          Sphinx de Gizeh

                                          Fiat S.p.A.