C
Chealsea S.
How would you check if a string is a valid hexadecimal code?
How would you check if a string is a valid hexadecimal code?
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
Avdi said: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
--
Avdi
Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
I tried "hex_pattern" with a string like "asdbgg" and it does not
produce an error, even though "g" isn't A-F 0-9.
Craig said: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
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.