Consider a simple integer digit identifying expression like this:
考虑一个简单的整数数字来识别这样的表达式
[0-9]+ printf("Integer");
Now if i give 123
as an input it returns Integer
, fair enough. Now if I give s123
as the input it prints out sInteger
. The unmatched s
is being printed by default ECHO
that's cool with me. But why is Integer
also printed. Shouldn't lex return just s
? My input is considered as a whole string right? I mean s123 is considered as a 1 full input
?. As soon as s
is encountered which does not match [0-9]+
so it should just echo default unmatched value s123
but why sInteger?
现在,如果我将123作为输入,则返回Integer,足够公平。现在,如果我将s123作为输入,则打印出sInteger。默认情况下ECHO正在打印无与伦比的s,这对我来说很酷。但为什么Integer也会打印出来。 lex不应该只返回s?我的输入被认为是一个完整的字符串吗?我的意思是s123被认为是一个完整的输入?一旦遇到与[0-9] +不匹配的s,那么它应该只回显默认的不匹配值s123,但为什么sInteger?
1 个解决方案
#1
1
The string s123
is being matched by the regex [0-9]+
. If you want to match strings which consist of only integers, you should try ^[0-9]+$
.
字符串s123由正则表达式[0-9] +匹配。如果要匹配仅包含整数的字符串,则应尝试^ [0-9] + $。
#1
1
The string s123
is being matched by the regex [0-9]+
. If you want to match strings which consist of only integers, you should try ^[0-9]+$
.
字符串s123由正则表达式[0-9] +匹配。如果要匹配仅包含整数的字符串,则应尝试^ [0-9] + $。