Why is this number matched with this regex?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question


















  • 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














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?







share|improve this question


















  • 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












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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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












  • 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










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)





share|improve this answer




















  • 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

















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]$





share|improve this answer






















  • 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




    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










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%2f422841%2fwhy-is-this-number-matched-with-this-regex%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
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)





share|improve this answer




















  • 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














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)





share|improve this answer




















  • 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












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)





share|improve this answer












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)






share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












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]$





share|improve this answer






















  • 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




    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














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]$





share|improve this answer






















  • 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




    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












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]$





share|improve this answer














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]$






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 8 at 16:37

























answered Feb 8 at 16:15









ilkkachu

49.6k673137




49.6k673137











  • 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




    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






  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

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

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay