grep regexp [A-Z] returns digits

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have file a.txt with its content as



1 (C##2)
2 (C##U)


and using grep on it



cat a.txt | grep '##[A-Z]*' 
1 (C##2)
2 (C##U)


Why did number 1 (C##2) appear in this grep result? I wanted only second one to
appear. So I have specified [A-Z]. Why did it take ..##2 as match?










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have file a.txt with its content as



    1 (C##2)
    2 (C##U)


    and using grep on it



    cat a.txt | grep '##[A-Z]*' 
    1 (C##2)
    2 (C##U)


    Why did number 1 (C##2) appear in this grep result? I wanted only second one to
    appear. So I have specified [A-Z]. Why did it take ..##2 as match?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have file a.txt with its content as



      1 (C##2)
      2 (C##U)


      and using grep on it



      cat a.txt | grep '##[A-Z]*' 
      1 (C##2)
      2 (C##U)


      Why did number 1 (C##2) appear in this grep result? I wanted only second one to
      appear. So I have specified [A-Z]. Why did it take ..##2 as match?










      share|improve this question















      I have file a.txt with its content as



      1 (C##2)
      2 (C##U)


      and using grep on it



      cat a.txt | grep '##[A-Z]*' 
      1 (C##2)
      2 (C##U)


      Why did number 1 (C##2) appear in this grep result? I wanted only second one to
      appear. So I have specified [A-Z]. Why did it take ..##2 as match?







      grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 5:42









      Inian

      3,785824




      3,785824










      asked Nov 20 at 5:34









      mandrake00

      294




      294




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          [A-Z]* is zero or more occurrences of [A-Z]. There are zero occurrences of [A-Z] in ##2, so the line matches. You probably want one or more (1, (or + with GNU grep or compatible), or + with the -E option enabling EREs), or simply ##[A-Z] as if it matches ##[A-Z], it also matches ##[A-Z]+ and vice-versa.



          Also note that except in the C/POSIX locale, what is matched by [A-Z] is unspecified and the list of characters (or even possibly collation elements made of several characters) it matches varies with the locale and operating system. On GNU systems, it's generally only characters of the Latin script (including things like Dž or É) often only uppercase ones, but sometimes also lowercase ones including the English a-z letters (like in the Thai locale for Thailand on Ubuntu 18.04 at least). You get much more exotic lists in some non-GNU systems like Solaris. To match on ABCDEFGHIJKLMNOPQRSTUVWXYZ only, use [ABCDEFGHIJKLMNOPQRSTUVWXYZ].






          share|improve this answer






















          • Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
            – mandrake00
            Nov 20 at 5:49






          • 1




            At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
            – muru
            Nov 20 at 5:54







          • 1




            @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
            – muru
            Nov 20 at 6:14










          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
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482887%2fgrep-regexp-a-z-returns-digits%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          [A-Z]* is zero or more occurrences of [A-Z]. There are zero occurrences of [A-Z] in ##2, so the line matches. You probably want one or more (1, (or + with GNU grep or compatible), or + with the -E option enabling EREs), or simply ##[A-Z] as if it matches ##[A-Z], it also matches ##[A-Z]+ and vice-versa.



          Also note that except in the C/POSIX locale, what is matched by [A-Z] is unspecified and the list of characters (or even possibly collation elements made of several characters) it matches varies with the locale and operating system. On GNU systems, it's generally only characters of the Latin script (including things like Dž or É) often only uppercase ones, but sometimes also lowercase ones including the English a-z letters (like in the Thai locale for Thailand on Ubuntu 18.04 at least). You get much more exotic lists in some non-GNU systems like Solaris. To match on ABCDEFGHIJKLMNOPQRSTUVWXYZ only, use [ABCDEFGHIJKLMNOPQRSTUVWXYZ].






          share|improve this answer






















          • Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
            – mandrake00
            Nov 20 at 5:49






          • 1




            At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
            – muru
            Nov 20 at 5:54







          • 1




            @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
            – muru
            Nov 20 at 6:14














          up vote
          5
          down vote













          [A-Z]* is zero or more occurrences of [A-Z]. There are zero occurrences of [A-Z] in ##2, so the line matches. You probably want one or more (1, (or + with GNU grep or compatible), or + with the -E option enabling EREs), or simply ##[A-Z] as if it matches ##[A-Z], it also matches ##[A-Z]+ and vice-versa.



          Also note that except in the C/POSIX locale, what is matched by [A-Z] is unspecified and the list of characters (or even possibly collation elements made of several characters) it matches varies with the locale and operating system. On GNU systems, it's generally only characters of the Latin script (including things like Dž or É) often only uppercase ones, but sometimes also lowercase ones including the English a-z letters (like in the Thai locale for Thailand on Ubuntu 18.04 at least). You get much more exotic lists in some non-GNU systems like Solaris. To match on ABCDEFGHIJKLMNOPQRSTUVWXYZ only, use [ABCDEFGHIJKLMNOPQRSTUVWXYZ].






          share|improve this answer






















          • Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
            – mandrake00
            Nov 20 at 5:49






          • 1




            At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
            – muru
            Nov 20 at 5:54







          • 1




            @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
            – muru
            Nov 20 at 6:14












          up vote
          5
          down vote










          up vote
          5
          down vote









          [A-Z]* is zero or more occurrences of [A-Z]. There are zero occurrences of [A-Z] in ##2, so the line matches. You probably want one or more (1, (or + with GNU grep or compatible), or + with the -E option enabling EREs), or simply ##[A-Z] as if it matches ##[A-Z], it also matches ##[A-Z]+ and vice-versa.



          Also note that except in the C/POSIX locale, what is matched by [A-Z] is unspecified and the list of characters (or even possibly collation elements made of several characters) it matches varies with the locale and operating system. On GNU systems, it's generally only characters of the Latin script (including things like Dž or É) often only uppercase ones, but sometimes also lowercase ones including the English a-z letters (like in the Thai locale for Thailand on Ubuntu 18.04 at least). You get much more exotic lists in some non-GNU systems like Solaris. To match on ABCDEFGHIJKLMNOPQRSTUVWXYZ only, use [ABCDEFGHIJKLMNOPQRSTUVWXYZ].






          share|improve this answer














          [A-Z]* is zero or more occurrences of [A-Z]. There are zero occurrences of [A-Z] in ##2, so the line matches. You probably want one or more (1, (or + with GNU grep or compatible), or + with the -E option enabling EREs), or simply ##[A-Z] as if it matches ##[A-Z], it also matches ##[A-Z]+ and vice-versa.



          Also note that except in the C/POSIX locale, what is matched by [A-Z] is unspecified and the list of characters (or even possibly collation elements made of several characters) it matches varies with the locale and operating system. On GNU systems, it's generally only characters of the Latin script (including things like Dž or É) often only uppercase ones, but sometimes also lowercase ones including the English a-z letters (like in the Thai locale for Thailand on Ubuntu 18.04 at least). You get much more exotic lists in some non-GNU systems like Solaris. To match on ABCDEFGHIJKLMNOPQRSTUVWXYZ only, use [ABCDEFGHIJKLMNOPQRSTUVWXYZ].







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 6:14









          Stéphane Chazelas

          294k54555898




          294k54555898










          answered Nov 20 at 5:43









          muru

          35.1k581154




          35.1k581154











          • Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
            – mandrake00
            Nov 20 at 5:49






          • 1




            At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
            – muru
            Nov 20 at 5:54







          • 1




            @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
            – muru
            Nov 20 at 6:14
















          • Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
            – mandrake00
            Nov 20 at 5:49






          • 1




            At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
            – muru
            Nov 20 at 5:54







          • 1




            @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
            – muru
            Nov 20 at 6:14















          Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
          – mandrake00
          Nov 20 at 5:49




          Thank you. ...grep '#[A-Z]+' works. But still amused by "Zero or more matches"... Is it helpful anywhere(any example)? searching for "zero matches" is same as not searching for it at all, isn't it?
          – mandrake00
          Nov 20 at 5:49




          1




          1




          At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
          – muru
          Nov 20 at 5:54





          At the start or end of a regex, it isn't that useful, but in between, sure. Say a[a-zA-Z]*_ .. a string that begins with a and has an underscore somewhere in it, but arbitrary alphabets in between.
          – muru
          Nov 20 at 5:54





          1




          1




          @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
          – muru
          Nov 20 at 6:14




          @mandrake00 do read Stéphane Chazelas's edit on the answer as well.
          – muru
          Nov 20 at 6:14

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482887%2fgrep-regexp-a-z-returns-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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?