Regular Expressions

Flashcard 1 of 10

Hexadecimal is the base 16 number system. It is typically written using all the base 10 numerals, as well as the letters "a" through "f".

We want to write a pattern to match all hexadecimal characters. As a start, we have the following pattern:

hex_pattern = \

# Valid matches
"123abd" =~ hex_pattern #=> 0
"456789" =~ hex_pattern #=> 0

# Invalid, not hex characters
"876jkl" =~ hex_pattern #=> nil

While the pattern does seem to work, it is a bit long and verbose. Is there any way we can simplify the pattern?


Reveal Answer