From: Craig Demyanovich Date: 2008-11-03T07:02:50+09:00 Subject: Re: check if valid hexadecimal code? ------=_Part_26966_18071187.1225663449978 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Sun, Nov 2, 2008 at 5:56 PM, Avdi Grimm wrote: > On Sun, Nov 2, 2008 at 4:30 PM, Chealsea S. 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 ------=_Part_26966_18071187.1225663449978--