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

Clash 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_]*$"
regular-expression
add a comment |Â
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_]*$"
regular-expression
add a comment |Â
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_]*$"
regular-expression
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_]*$"
regular-expression
asked Feb 18 at 15:27
illiteracy
918
918
add a comment |Â
add a comment |Â
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 "]" .
add a comment |Â
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.
add a comment |Â
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 "]" .
add a comment |Â
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 "]" .
add a comment |Â
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 "]" .
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 "]" .
answered Feb 18 at 15:37
cg909
2,4711020
2,4711020
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Feb 18 at 15:36
Tomasz
8,04052560
8,04052560
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password