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 = \
  /^(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+$/

# 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?

Answer:

Reveal Answer