I have to check if a 4 character string is all valid hex, I found another question which demonstrates exactly what I want to do but it's Java: Regex to check string contains only Hex characters
How can I accomplish this?
I read the ruby docs for Regular expressions, but I don't understand how to return a true or false based on this match?
In ruby regex \h matches a hex digit and \H matches a non-hex digit.
So
!str[/\H/]
is what you're looking for.
if str =~ /^[0-9A-F]+$/
does the trick. If you want case insensitive then:
str =~ /^[0-9A-F]+$/i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With