Craig Demyanovich
11/2/2008 10:03:00 PM
[Note: parts of this message were removed to make it a legal post.]
On Sun, Nov 2, 2008 at 5:56 PM, Avdi Grimm <avdi@avdi.org> wrote:
> On Sun, Nov 2, 2008 at 4:30 PM, Chealsea S. <youngliars@gmail.com> wrote:
> > How would you check if a string is a valid hexadecimal code?
>
> irb(main):007:0> hex_pattern = /^[[:xdigit:]]+$/
> => /^[[:xdigit:]]+$/
> irb(main):008:0> hex_pattern === "0123456789ABCDEF"
> => true
> irb(main):009:0> hex_pattern === "Chunky Bacon"
> => false
I wonder if you should use /\A[[:xdigit:]]+\z/ to match the beginning and
end of the string instead of the beginning and end of the line.
Another options is to use String#hex method and check the return value,
though I find it weird that it returns 0 on error, since "0x0".hex also
returns 0
Regards,
Craig