Why regular expression '[a-z][0-9]+$' not work











up vote
0
down vote

favorite












I use the regular expression to handle the string "abc123".
The command below is work and return value "c123"



echo abc123 | grep -o [a-z][0-9]*$


But the command below does not work.



echo abc123 | grep -o [a-z][0-9]+$


Why do I get this result?



I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.



So this situation makes me confused.










share|improve this question









New contributor




IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I use the regular expression to handle the string "abc123".
    The command below is work and return value "c123"



    echo abc123 | grep -o [a-z][0-9]*$


    But the command below does not work.



    echo abc123 | grep -o [a-z][0-9]+$


    Why do I get this result?



    I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.



    So this situation makes me confused.










    share|improve this question









    New contributor




    IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I use the regular expression to handle the string "abc123".
      The command below is work and return value "c123"



      echo abc123 | grep -o [a-z][0-9]*$


      But the command below does not work.



      echo abc123 | grep -o [a-z][0-9]+$


      Why do I get this result?



      I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.



      So this situation makes me confused.










      share|improve this question









      New contributor




      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I use the regular expression to handle the string "abc123".
      The command below is work and return value "c123"



      echo abc123 | grep -o [a-z][0-9]*$


      But the command below does not work.



      echo abc123 | grep -o [a-z][0-9]+$


      Why do I get this result?



      I knew the '*' is used to matches the preceding pattern element zero or more time, and '+' is used to matches the preceding pattern element at least one or more time.



      So this situation makes me confused.







      regular-expression






      share|improve this question









      New contributor




      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      38.5k1479128




      38.5k1479128






      New contributor




      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      IsaraSu

      31




      31




      New contributor




      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      IsaraSu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          + is only a quantifier in extended regular expressions (ERE):



          $ echo abc123 | grep -Eo '[a-z][0-9]+$'
          c123


          In basic regular expressions (BRE) it matches literal +, although you can use {1,} instead, or in GNU grep (-o is already a GNU extension anyway), +:



          $ echo abc123 | grep -o '[a-z][0-9]+$'
          c123


          (note the quotes to prevent [ and from being interpreted by the shell).






          share|improve this answer























          • Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
            – Kusalananda
            yesterday












          • @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
            – Stéphane Chazelas
            yesterday




















          up vote
          0
          down vote













          + in grep need to be escaped to take effect. Instead of



          echo abc123 | grep -o [a-z][0-9]+$


          You need to write



          echo abc123 | grep -o '[a-z][0-9]+$'


          There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.



          You can also use egrep which is a synonym of grep -E and uses Extended RE, as commented by @muru.






          share|improve this answer























          • Or use ERE with -E
            – muru
            2 days ago










          • The escaped + in basic regular expressions is a non-standard GNU extension.
            – Kusalananda
            2 days ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          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',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });






          IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487484%2fwhy-regular-expression-a-z0-9-not-work%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








          up vote
          4
          down vote



          accepted










          + is only a quantifier in extended regular expressions (ERE):



          $ echo abc123 | grep -Eo '[a-z][0-9]+$'
          c123


          In basic regular expressions (BRE) it matches literal +, although you can use {1,} instead, or in GNU grep (-o is already a GNU extension anyway), +:



          $ echo abc123 | grep -o '[a-z][0-9]+$'
          c123


          (note the quotes to prevent [ and from being interpreted by the shell).






          share|improve this answer























          • Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
            – Kusalananda
            yesterday












          • @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
            – Stéphane Chazelas
            yesterday

















          up vote
          4
          down vote



          accepted










          + is only a quantifier in extended regular expressions (ERE):



          $ echo abc123 | grep -Eo '[a-z][0-9]+$'
          c123


          In basic regular expressions (BRE) it matches literal +, although you can use {1,} instead, or in GNU grep (-o is already a GNU extension anyway), +:



          $ echo abc123 | grep -o '[a-z][0-9]+$'
          c123


          (note the quotes to prevent [ and from being interpreted by the shell).






          share|improve this answer























          • Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
            – Kusalananda
            yesterday












          • @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
            – Stéphane Chazelas
            yesterday















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          + is only a quantifier in extended regular expressions (ERE):



          $ echo abc123 | grep -Eo '[a-z][0-9]+$'
          c123


          In basic regular expressions (BRE) it matches literal +, although you can use {1,} instead, or in GNU grep (-o is already a GNU extension anyway), +:



          $ echo abc123 | grep -o '[a-z][0-9]+$'
          c123


          (note the quotes to prevent [ and from being interpreted by the shell).






          share|improve this answer














          + is only a quantifier in extended regular expressions (ERE):



          $ echo abc123 | grep -Eo '[a-z][0-9]+$'
          c123


          In basic regular expressions (BRE) it matches literal +, although you can use {1,} instead, or in GNU grep (-o is already a GNU extension anyway), +:



          $ echo abc123 | grep -o '[a-z][0-9]+$'
          c123


          (note the quotes to prevent [ and from being interpreted by the shell).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday









          Stéphane Chazelas

          297k54562907




          297k54562907










          answered 2 days ago









          steeldriver

          34.1k34983




          34.1k34983












          • Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
            – Kusalananda
            yesterday












          • @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
            – Stéphane Chazelas
            yesterday




















          • Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
            – Kusalananda
            yesterday












          • @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
            – Stéphane Chazelas
            yesterday


















          Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
          – Kusalananda
          yesterday






          Just a comment on @StéphaneChazelas' edit: -o is a GNU extension, but it's also available in (some) other grep implementations. However, + in a basic regular expression is GNU-only as far as I know.
          – Kusalananda
          yesterday














          @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
          – Stéphane Chazelas
          yesterday






          @Kusalananda, I agree it's a bit of a shortcut. Several other implementations have picked some of the GNU extensions over the years. + is also found in some non-GNU implementations like busybox or ast-open. Also note that many BSDs still use a fork of an old GNU grep, so will support old GNU extensions like those.
          – Stéphane Chazelas
          yesterday














          up vote
          0
          down vote













          + in grep need to be escaped to take effect. Instead of



          echo abc123 | grep -o [a-z][0-9]+$


          You need to write



          echo abc123 | grep -o '[a-z][0-9]+$'


          There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.



          You can also use egrep which is a synonym of grep -E and uses Extended RE, as commented by @muru.






          share|improve this answer























          • Or use ERE with -E
            – muru
            2 days ago










          • The escaped + in basic regular expressions is a non-standard GNU extension.
            – Kusalananda
            2 days ago















          up vote
          0
          down vote













          + in grep need to be escaped to take effect. Instead of



          echo abc123 | grep -o [a-z][0-9]+$


          You need to write



          echo abc123 | grep -o '[a-z][0-9]+$'


          There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.



          You can also use egrep which is a synonym of grep -E and uses Extended RE, as commented by @muru.






          share|improve this answer























          • Or use ERE with -E
            – muru
            2 days ago










          • The escaped + in basic regular expressions is a non-standard GNU extension.
            – Kusalananda
            2 days ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          + in grep need to be escaped to take effect. Instead of



          echo abc123 | grep -o [a-z][0-9]+$


          You need to write



          echo abc123 | grep -o '[a-z][0-9]+$'


          There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.



          You can also use egrep which is a synonym of grep -E and uses Extended RE, as commented by @muru.






          share|improve this answer














          + in grep need to be escaped to take effect. Instead of



          echo abc123 | grep -o [a-z][0-9]+$


          You need to write



          echo abc123 | grep -o '[a-z][0-9]+$'


          There are other characters that need to be escaped as well. It is also good practice to put your regex in single quotes.



          You can also use egrep which is a synonym of grep -E and uses Extended RE, as commented by @muru.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Weijun Zhou

          1,468224




          1,468224












          • Or use ERE with -E
            – muru
            2 days ago










          • The escaped + in basic regular expressions is a non-standard GNU extension.
            – Kusalananda
            2 days ago


















          • Or use ERE with -E
            – muru
            2 days ago










          • The escaped + in basic regular expressions is a non-standard GNU extension.
            – Kusalananda
            2 days ago
















          Or use ERE with -E
          – muru
          2 days ago




          Or use ERE with -E
          – muru
          2 days ago












          The escaped + in basic regular expressions is a non-standard GNU extension.
          – Kusalananda
          2 days ago




          The escaped + in basic regular expressions is a non-standard GNU extension.
          – Kusalananda
          2 days ago










          IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.













          IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.












          IsaraSu is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • 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%2funix.stackexchange.com%2fquestions%2f487484%2fwhy-regular-expression-a-z0-9-not-work%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

          Accessing regular linux commands in Huawei's Dopra Linux

          Can't connect RFCOMM socket: Host is down

          Kernel panic - not syncing: Fatal Exception in Interrupt