What is difference between [-a-z] and [a-z] in regular expression?

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











up vote
0
down vote

favorite












What is difference between [-a-z] and [a-z] in regular expression?
What is the first minus meaning in [-a-z]?



From default /etc/adduser.conf(For example):



#check user and group names also against this regular expression.`
#NAME_REGEX="^[a-z][-a-z0-9_]*$"






share|improve this question
























    up vote
    0
    down vote

    favorite












    What is difference between [-a-z] and [a-z] in regular expression?
    What is the first minus meaning in [-a-z]?



    From default /etc/adduser.conf(For example):



    #check user and group names also against this regular expression.`
    #NAME_REGEX="^[a-z][-a-z0-9_]*$"






    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      What is difference between [-a-z] and [a-z] in regular expression?
      What is the first minus meaning in [-a-z]?



      From default /etc/adduser.conf(For example):



      #check user and group names also against this regular expression.`
      #NAME_REGEX="^[a-z][-a-z0-9_]*$"






      share|improve this question












      What is difference between [-a-z] and [a-z] in regular expression?
      What is the first minus meaning in [-a-z]?



      From default /etc/adduser.conf(For example):



      #check user and group names also against this regular expression.`
      #NAME_REGEX="^[a-z][-a-z0-9_]*$"








      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 18 at 15:27









      illiteracy

      918




      918




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          The hyphen has only a special meaning when it's not the first or last character in a bracketed character class.



          [a-z] matches all ASCII lower-case letters from a to z. [-a-z] matches all ASCII lower-case letters from a to z and the hyphen -.



          This is documented in the Perl documentation:




          Within a list, the "-" character specifies a range of characters, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^" ), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]" .







          share|improve this answer



























            up vote
            0
            down vote













            The minus at the beginning is used to include the literal minus character in the group. It might also be at the end of the group. Compare:



            $ echo abc- | grep [w-x]

            $ echo abc- | grep [w-x-]
            abc-
            $ echo abc- | grep [-w-x]
            abc-


            When it's not at the beginning or at the end, it's function is to describe a range, as in w-x.






            share|improve this answer




















              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: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f424977%2fwhat-is-difference-between-a-z-and-a-z-in-regular-expression%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              The hyphen has only a special meaning when it's not the first or last character in a bracketed character class.



              [a-z] matches all ASCII lower-case letters from a to z. [-a-z] matches all ASCII lower-case letters from a to z and the hyphen -.



              This is documented in the Perl documentation:




              Within a list, the "-" character specifies a range of characters, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^" ), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]" .







              share|improve this answer
























                up vote
                2
                down vote



                accepted










                The hyphen has only a special meaning when it's not the first or last character in a bracketed character class.



                [a-z] matches all ASCII lower-case letters from a to z. [-a-z] matches all ASCII lower-case letters from a to z and the hyphen -.



                This is documented in the Perl documentation:




                Within a list, the "-" character specifies a range of characters, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^" ), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]" .







                share|improve this answer






















                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  The hyphen has only a special meaning when it's not the first or last character in a bracketed character class.



                  [a-z] matches all ASCII lower-case letters from a to z. [-a-z] matches all ASCII lower-case letters from a to z and the hyphen -.



                  This is documented in the Perl documentation:




                  Within a list, the "-" character specifies a range of characters, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^" ), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]" .







                  share|improve this answer












                  The hyphen has only a special meaning when it's not the first or last character in a bracketed character class.



                  [a-z] matches all ASCII lower-case letters from a to z. [-a-z] matches all ASCII lower-case letters from a to z and the hyphen -.



                  This is documented in the Perl documentation:




                  Within a list, the "-" character specifies a range of characters, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^" ), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]" .








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 18 at 15:37









                  cg909

                  2,4711020




                  2,4711020






















                      up vote
                      0
                      down vote













                      The minus at the beginning is used to include the literal minus character in the group. It might also be at the end of the group. Compare:



                      $ echo abc- | grep [w-x]

                      $ echo abc- | grep [w-x-]
                      abc-
                      $ echo abc- | grep [-w-x]
                      abc-


                      When it's not at the beginning or at the end, it's function is to describe a range, as in w-x.






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        The minus at the beginning is used to include the literal minus character in the group. It might also be at the end of the group. Compare:



                        $ echo abc- | grep [w-x]

                        $ echo abc- | grep [w-x-]
                        abc-
                        $ echo abc- | grep [-w-x]
                        abc-


                        When it's not at the beginning or at the end, it's function is to describe a range, as in w-x.






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          The minus at the beginning is used to include the literal minus character in the group. It might also be at the end of the group. Compare:



                          $ echo abc- | grep [w-x]

                          $ echo abc- | grep [w-x-]
                          abc-
                          $ echo abc- | grep [-w-x]
                          abc-


                          When it's not at the beginning or at the end, it's function is to describe a range, as in w-x.






                          share|improve this answer












                          The minus at the beginning is used to include the literal minus character in the group. It might also be at the end of the group. Compare:



                          $ echo abc- | grep [w-x]

                          $ echo abc- | grep [w-x-]
                          abc-
                          $ echo abc- | grep [-w-x]
                          abc-


                          When it's not at the beginning or at the end, it's function is to describe a range, as in w-x.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 18 at 15:36









                          Tomasz

                          8,04052560




                          8,04052560






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424977%2fwhat-is-difference-between-a-z-and-a-z-in-regular-expression%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)