...

View Full Version : regex not doing as I expect.



bazz
06-01-2009, 09:52 AM
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

bazz
06-01-2009, 09:56 AM
got it !!



unless ( $value =~ /^\d{1,2}$/ ) { exit; }



bazz

KevinADC
06-01-2009, 06: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.

bazz
06-01-2009, 08:33 PM
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, 09: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#[@!\#]#) {...}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum