Gossamer Forum
Home : General : Perl Programming :

regex help - test valid bgcolor

Quote Reply
regex help - test valid bgcolor
Hey all,

I'm pulling my hair out on this one and yet it seems so simple. I have a script I'm working on that takes input for a bgcolor value and I want to test the input to make sure it's in a valid format... Here's what I have so far.

$color =~ /\d{6,7}/ && /^(\#?)[0-9a-f_A-F]{6}/

Basically, I want to see if the field is 6 or 7 characters long, may or may not start with a '#' and the remaining characters are all either letters a-f or numbers 0-9. It just keeps returning an invalid entry no matter what I do.

Anyone have an idea as to what I'm missing...

Jerry


Quote Reply
Re: regex help - test valid bgcolor In reply to
$color =~ /^#?[0-9A-F]{6}$/i;

bind $color to regex /^#?[0-9A-F]{6}$/i..
^ -> start of the line in the string..
#? -> zero or one #
[0-9A-F]{6} -> 6 characters 0123456789ABCDEF
$ -> end of the line in the string..

i -> i modifier.. insensitive cases matchs a-f with A-F

about yours....

$color =~ /\d{6,7}/ && /^(\#?)[0-9a-f_A-F]{6}/

\d is a digit.. {6,7} means min 6 max 7.. there can't be 7 digits in the hex rgb.. plus there is always the letters.. a-f..

second.. the format doesn't appear to be correct.. you binded an expression to the scalar $color..
the second regex is.. /^(\#?)[0-9a-f_A-F]{6}/..

(\#?).. # doesn't need to be escaped.. and the ( and ) put it in the buffer.. ? means zero or one..
[0-9a-f_A-F]{6}.. i see where you were going with this.. close.. it would work.. but it would also allow '#______' because you put an underscore in the list of accepted characters..

Jerry Su