grep regexp [A-Z] returns digits
Clash 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?
grep
add a comment |
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?
grep
add a comment |
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?
grep
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
grep
edited Nov 20 at 5:42
Inian
3,785824
3,785824
asked Nov 20 at 5:34
mandrake00
294
294
add a comment |
add a comment |
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]
.
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. Saya[a-zA-Z]*_
.. a string that begins witha
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
add a comment |
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]
.
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. Saya[a-zA-Z]*_
.. a string that begins witha
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
add a comment |
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]
.
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. Saya[a-zA-Z]*_
.. a string that begins witha
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
add a comment |
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]
.
[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]
.
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. Saya[a-zA-Z]*_
.. a string that begins witha
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
add a comment |
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. Saya[a-zA-Z]*_
.. a string that begins witha
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
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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