View Full Version : regex not doing as I expect.
Hi,
I want to check a number being submitted to make sure that it comprises either 1 or two digits. ideally I'd like to make sure it is between 1 and 35.
what I have done should (I thought) have made sure that the number submitted would have one or two digits. but it errors on 10. it accepts 8.
unless ( $value =~ /^[\d{1,2}]$/ ) { exit; }
what have I done wrongly.
bazz
got it !!
unless ( $value =~ /^\d{1,2}$/ ) { exit; }
bazz
KevinADC
06-01-2009, 07:27 PM
got it !!
unless ( $value =~ /^\d{1,2}$/ ) { exit; }
bazz
Few things have the same meta meaning inside a character class as they do outside a character class. The list of special characters inside of a character class is very short:
-]\^$
And some of those only have special meaning if they are used at a specific location inside the character class.
Thanks Kevin.
I know some of the effects of those inside a character class but not the others:
^ means not in
- hyphen must come first
\ escapes the next character
what do the ] and $ do when positioned within a character class?
bazz
KevinADC
06-01-2009, 10:21 PM
The '$' symbol is the same as outside, its a scalar datatype symbol.
The ']' symbol is the end of a character class symbol, so in some usages inside a character class it will need to be escaped so its not interprted as the end of the character class.
The '-' is the same as outside a character class, its the range operator, as in a-zA-Z0-9. So you have to escape it if its usage inside a character class can be interpreted as the range operator: [a\-z] but if you write it like this there is no need to escape it: [az-] because the context is clear: its not a range.
The '^' symbol is the negated character class symbol if it is the very first character in a character class, otherwise its just a literal ^ symbol.
The other special characters are whatever you use as the regexp delimiters, so if you used # you need to escape it:
if (m#[@!\#]#) {...}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.