RegEx for a string of length 0-2 -
i'm trying match string can either empty or have 1 or 2 numbers in such following:
"" (empty) "1" "23"
string more numbers or non-numeric characters should not match. closest guess regex:
[0-9]{0,2}
which read "the numbers 0 through 9 occurring 0 2 times." however, in practice find regex matches longer strings "333". how possible restrict string length in regular expressions?
use following regex:
^[0-9]{0,2}$
you had -- ^
, $
characters anchors match beginning , end of string, respectively.
for more in-depth discussion on anchors, see here:
[anchors] not match character @ all. instead, match position before, after or between characters. can used "anchor" regex match @ position.
Comments
Post a Comment