Why is this number matched with this regex?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
The regex is -?([0-9]|([1-9][0-9]))
.
The number is -2231
and it's being matched. From my understanding, it should be a single digit or double digits.ÃÂ
Why is this number matched with this regex?
regular-expression
add a comment |Â
up vote
3
down vote
favorite
The regex is -?([0-9]|([1-9][0-9]))
.
The number is -2231
and it's being matched. From my understanding, it should be a single digit or double digits.ÃÂ
Why is this number matched with this regex?
regular-expression
2
It has 4 matches: See the explanation online.
â Thomas Weller
Feb 8 at 21:56
5
A match can be found in the string, but it doesn't match the whole string.
â martin
Feb 9 at 1:20
@Jeff Schaller:â The original version of this question contained a regular expression in (ordinary, single) quotes, and you added backâÂÂticks without removing the quotes.â That makes it look like the single quotes are part of the regex that the OP is asking about, which makes the question very confusing.
â G-Man
Feb 16 at 4:57
Sorry for the confusion, @G-man. IâÂÂm accustomed to seeing regexes in quotes to protect them from shell interpretation. The number wouldnâÂÂt have matched a regex that required quote marks, but itâÂÂs better to be clear about things, so thank you for the improvement!
â Jeff Schaller
Feb 16 at 11:00
@Jeff Schaller:â Yeah, it occurred to me later that, if I had seen the regex in quotes in the context of a command line, it would have been perfectly clear.
â G-Man
Feb 16 at 15:13
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
The regex is -?([0-9]|([1-9][0-9]))
.
The number is -2231
and it's being matched. From my understanding, it should be a single digit or double digits.ÃÂ
Why is this number matched with this regex?
regular-expression
The regex is -?([0-9]|([1-9][0-9]))
.
The number is -2231
and it's being matched. From my understanding, it should be a single digit or double digits.ÃÂ
Why is this number matched with this regex?
regular-expression
edited Feb 16 at 4:57
G-Man
11.5k82656
11.5k82656
asked Feb 8 at 15:45
Rashad
568
568
2
It has 4 matches: See the explanation online.
â Thomas Weller
Feb 8 at 21:56
5
A match can be found in the string, but it doesn't match the whole string.
â martin
Feb 9 at 1:20
@Jeff Schaller:â The original version of this question contained a regular expression in (ordinary, single) quotes, and you added backâÂÂticks without removing the quotes.â That makes it look like the single quotes are part of the regex that the OP is asking about, which makes the question very confusing.
â G-Man
Feb 16 at 4:57
Sorry for the confusion, @G-man. IâÂÂm accustomed to seeing regexes in quotes to protect them from shell interpretation. The number wouldnâÂÂt have matched a regex that required quote marks, but itâÂÂs better to be clear about things, so thank you for the improvement!
â Jeff Schaller
Feb 16 at 11:00
@Jeff Schaller:â Yeah, it occurred to me later that, if I had seen the regex in quotes in the context of a command line, it would have been perfectly clear.
â G-Man
Feb 16 at 15:13
add a comment |Â
2
It has 4 matches: See the explanation online.
â Thomas Weller
Feb 8 at 21:56
5
A match can be found in the string, but it doesn't match the whole string.
â martin
Feb 9 at 1:20
@Jeff Schaller:â The original version of this question contained a regular expression in (ordinary, single) quotes, and you added backâÂÂticks without removing the quotes.â That makes it look like the single quotes are part of the regex that the OP is asking about, which makes the question very confusing.
â G-Man
Feb 16 at 4:57
Sorry for the confusion, @G-man. IâÂÂm accustomed to seeing regexes in quotes to protect them from shell interpretation. The number wouldnâÂÂt have matched a regex that required quote marks, but itâÂÂs better to be clear about things, so thank you for the improvement!
â Jeff Schaller
Feb 16 at 11:00
@Jeff Schaller:â Yeah, it occurred to me later that, if I had seen the regex in quotes in the context of a command line, it would have been perfectly clear.
â G-Man
Feb 16 at 15:13
2
2
It has 4 matches: See the explanation online.
â Thomas Weller
Feb 8 at 21:56
It has 4 matches: See the explanation online.
â Thomas Weller
Feb 8 at 21:56
5
5
A match can be found in the string, but it doesn't match the whole string.
â martin
Feb 9 at 1:20
A match can be found in the string, but it doesn't match the whole string.
â martin
Feb 9 at 1:20
@Jeff Schaller:â The original version of this question contained a regular expression in (ordinary, single) quotes, and you added backâÂÂticks without removing the quotes.â That makes it look like the single quotes are part of the regex that the OP is asking about, which makes the question very confusing.
â G-Man
Feb 16 at 4:57
@Jeff Schaller:â The original version of this question contained a regular expression in (ordinary, single) quotes, and you added backâÂÂticks without removing the quotes.â That makes it look like the single quotes are part of the regex that the OP is asking about, which makes the question very confusing.
â G-Man
Feb 16 at 4:57
Sorry for the confusion, @G-man. IâÂÂm accustomed to seeing regexes in quotes to protect them from shell interpretation. The number wouldnâÂÂt have matched a regex that required quote marks, but itâÂÂs better to be clear about things, so thank you for the improvement!
â Jeff Schaller
Feb 16 at 11:00
Sorry for the confusion, @G-man. IâÂÂm accustomed to seeing regexes in quotes to protect them from shell interpretation. The number wouldnâÂÂt have matched a regex that required quote marks, but itâÂÂs better to be clear about things, so thank you for the improvement!
â Jeff Schaller
Feb 16 at 11:00
@Jeff Schaller:â Yeah, it occurred to me later that, if I had seen the regex in quotes in the context of a command line, it would have been perfectly clear.
â G-Man
Feb 16 at 15:13
@Jeff Schaller:â Yeah, it occurred to me later that, if I had seen the regex in quotes in the context of a command line, it would have been perfectly clear.
â G-Man
Feb 16 at 15:13
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
21
down vote
accepted
The regular expression is not anchored, so it's free to match the first 1 or two numbers and "succeed", leaving the trailing numbers (successfully) unmatched.
If you require 1 or 2 digit numbers, anchor the regex:
'^-?([0-9]|([1-9][0-9]))$'
Some examples:
$ seq -100 -99 | grep -E '^-?([0-9]|[1-9][0-9])$'
-99
$ seq 99 100 | grep -E '^-?([0-9]|[1-9][0-9])$'
99
$ seq -9 9 | grep -E '^-?([0-9]|[1-9][0-9])$'
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
$ seq -2231 -100 | grep -E '^-?([0-9]|[1-9][0-9])$'
(empty)
See also the-x
grep
option to anchor the regexp at start and end.
â Stéphane Chazelas
Feb 8 at 15:57
4
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
add a comment |Â
up vote
14
down vote
Most programs that use regex patterns actually implement a search of the pattern, instead of a full-string match. Python has distinct search()
and match()
methods where search()
matches anywhere in the string, and match()
only at the beginning. grep
has the -x
option to demand a match against the whole string; by default it matches anywhere in the string. Others, like sed
, awk
and Perl will happily look for the pattern anywhere in the string. Use the ^
and $
modifiers ("anchors") to force the pattern to the start or end of the string (respectively).
So, the ERE pattern you want is probably this:
^-?[1-9]?[0-9]$
See alsoexpr
that anchors at the start but not at the end (uses BREs though, not those EREs).
â Stéphane Chazelas
Feb 8 at 16:17
5
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
21
down vote
accepted
The regular expression is not anchored, so it's free to match the first 1 or two numbers and "succeed", leaving the trailing numbers (successfully) unmatched.
If you require 1 or 2 digit numbers, anchor the regex:
'^-?([0-9]|([1-9][0-9]))$'
Some examples:
$ seq -100 -99 | grep -E '^-?([0-9]|[1-9][0-9])$'
-99
$ seq 99 100 | grep -E '^-?([0-9]|[1-9][0-9])$'
99
$ seq -9 9 | grep -E '^-?([0-9]|[1-9][0-9])$'
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
$ seq -2231 -100 | grep -E '^-?([0-9]|[1-9][0-9])$'
(empty)
See also the-x
grep
option to anchor the regexp at start and end.
â Stéphane Chazelas
Feb 8 at 15:57
4
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
add a comment |Â
up vote
21
down vote
accepted
The regular expression is not anchored, so it's free to match the first 1 or two numbers and "succeed", leaving the trailing numbers (successfully) unmatched.
If you require 1 or 2 digit numbers, anchor the regex:
'^-?([0-9]|([1-9][0-9]))$'
Some examples:
$ seq -100 -99 | grep -E '^-?([0-9]|[1-9][0-9])$'
-99
$ seq 99 100 | grep -E '^-?([0-9]|[1-9][0-9])$'
99
$ seq -9 9 | grep -E '^-?([0-9]|[1-9][0-9])$'
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
$ seq -2231 -100 | grep -E '^-?([0-9]|[1-9][0-9])$'
(empty)
See also the-x
grep
option to anchor the regexp at start and end.
â Stéphane Chazelas
Feb 8 at 15:57
4
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
add a comment |Â
up vote
21
down vote
accepted
up vote
21
down vote
accepted
The regular expression is not anchored, so it's free to match the first 1 or two numbers and "succeed", leaving the trailing numbers (successfully) unmatched.
If you require 1 or 2 digit numbers, anchor the regex:
'^-?([0-9]|([1-9][0-9]))$'
Some examples:
$ seq -100 -99 | grep -E '^-?([0-9]|[1-9][0-9])$'
-99
$ seq 99 100 | grep -E '^-?([0-9]|[1-9][0-9])$'
99
$ seq -9 9 | grep -E '^-?([0-9]|[1-9][0-9])$'
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
$ seq -2231 -100 | grep -E '^-?([0-9]|[1-9][0-9])$'
(empty)
The regular expression is not anchored, so it's free to match the first 1 or two numbers and "succeed", leaving the trailing numbers (successfully) unmatched.
If you require 1 or 2 digit numbers, anchor the regex:
'^-?([0-9]|([1-9][0-9]))$'
Some examples:
$ seq -100 -99 | grep -E '^-?([0-9]|[1-9][0-9])$'
-99
$ seq 99 100 | grep -E '^-?([0-9]|[1-9][0-9])$'
99
$ seq -9 9 | grep -E '^-?([0-9]|[1-9][0-9])$'
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
$ seq -2231 -100 | grep -E '^-?([0-9]|[1-9][0-9])$'
(empty)
answered Feb 8 at 15:55
Jeff Schaller
31.3k846105
31.3k846105
See also the-x
grep
option to anchor the regexp at start and end.
â Stéphane Chazelas
Feb 8 at 15:57
4
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
add a comment |Â
See also the-x
grep
option to anchor the regexp at start and end.
â Stéphane Chazelas
Feb 8 at 15:57
4
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
See also the
-x
grep
option to anchor the regexp at start and end.â Stéphane Chazelas
Feb 8 at 15:57
See also the
-x
grep
option to anchor the regexp at start and end.â Stéphane Chazelas
Feb 8 at 15:57
4
4
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
Note that $ anchors to end of line. If you want to anchor to the end of the "word", use b (if that it supported by your regex flavor).
â user3067860
Feb 8 at 18:31
add a comment |Â
up vote
14
down vote
Most programs that use regex patterns actually implement a search of the pattern, instead of a full-string match. Python has distinct search()
and match()
methods where search()
matches anywhere in the string, and match()
only at the beginning. grep
has the -x
option to demand a match against the whole string; by default it matches anywhere in the string. Others, like sed
, awk
and Perl will happily look for the pattern anywhere in the string. Use the ^
and $
modifiers ("anchors") to force the pattern to the start or end of the string (respectively).
So, the ERE pattern you want is probably this:
^-?[1-9]?[0-9]$
See alsoexpr
that anchors at the start but not at the end (uses BREs though, not those EREs).
â Stéphane Chazelas
Feb 8 at 16:17
5
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
add a comment |Â
up vote
14
down vote
Most programs that use regex patterns actually implement a search of the pattern, instead of a full-string match. Python has distinct search()
and match()
methods where search()
matches anywhere in the string, and match()
only at the beginning. grep
has the -x
option to demand a match against the whole string; by default it matches anywhere in the string. Others, like sed
, awk
and Perl will happily look for the pattern anywhere in the string. Use the ^
and $
modifiers ("anchors") to force the pattern to the start or end of the string (respectively).
So, the ERE pattern you want is probably this:
^-?[1-9]?[0-9]$
See alsoexpr
that anchors at the start but not at the end (uses BREs though, not those EREs).
â Stéphane Chazelas
Feb 8 at 16:17
5
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
add a comment |Â
up vote
14
down vote
up vote
14
down vote
Most programs that use regex patterns actually implement a search of the pattern, instead of a full-string match. Python has distinct search()
and match()
methods where search()
matches anywhere in the string, and match()
only at the beginning. grep
has the -x
option to demand a match against the whole string; by default it matches anywhere in the string. Others, like sed
, awk
and Perl will happily look for the pattern anywhere in the string. Use the ^
and $
modifiers ("anchors") to force the pattern to the start or end of the string (respectively).
So, the ERE pattern you want is probably this:
^-?[1-9]?[0-9]$
Most programs that use regex patterns actually implement a search of the pattern, instead of a full-string match. Python has distinct search()
and match()
methods where search()
matches anywhere in the string, and match()
only at the beginning. grep
has the -x
option to demand a match against the whole string; by default it matches anywhere in the string. Others, like sed
, awk
and Perl will happily look for the pattern anywhere in the string. Use the ^
and $
modifiers ("anchors") to force the pattern to the start or end of the string (respectively).
So, the ERE pattern you want is probably this:
^-?[1-9]?[0-9]$
edited Feb 8 at 16:37
answered Feb 8 at 16:15
ilkkachu
49.6k673137
49.6k673137
See alsoexpr
that anchors at the start but not at the end (uses BREs though, not those EREs).
â Stéphane Chazelas
Feb 8 at 16:17
5
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
add a comment |Â
See alsoexpr
that anchors at the start but not at the end (uses BREs though, not those EREs).
â Stéphane Chazelas
Feb 8 at 16:17
5
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
See also
expr
that anchors at the start but not at the end (uses BREs though, not those EREs).â Stéphane Chazelas
Feb 8 at 16:17
See also
expr
that anchors at the start but not at the end (uses BREs though, not those EREs).â Stéphane Chazelas
Feb 8 at 16:17
5
5
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
I like this answer because it explains that regular expressions can only be evaluated in the context in which they're used. Sometimes the regex must match the entire string, other times it must only be found somewhere within another string. The OP's question can't be answered without understand what program or function the regex is used within.
â Randall Stewart
Feb 8 at 22:12
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
i just put the number in vi editor
â Rashad
Feb 11 at 0:54
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%2f422841%2fwhy-is-this-number-matched-with-this-regex%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
2
It has 4 matches: See the explanation online.
â Thomas Weller
Feb 8 at 21:56
5
A match can be found in the string, but it doesn't match the whole string.
â martin
Feb 9 at 1:20
@Jeff Schaller:â The original version of this question contained a regular expression in (ordinary, single) quotes, and you added backâÂÂticks without removing the quotes.â That makes it look like the single quotes are part of the regex that the OP is asking about, which makes the question very confusing.
â G-Man
Feb 16 at 4:57
Sorry for the confusion, @G-man. IâÂÂm accustomed to seeing regexes in quotes to protect them from shell interpretation. The number wouldnâÂÂt have matched a regex that required quote marks, but itâÂÂs better to be clear about things, so thank you for the improvement!
â Jeff Schaller
Feb 16 at 11:00
@Jeff Schaller:â Yeah, it occurred to me later that, if I had seen the regex in quotes in the context of a command line, it would have been perfectly clear.
â G-Man
Feb 16 at 15:13