9951 explained code solutions for 126 technologies


python-regexHow to match a hex number with regex in Python?


Regex can be used to match a hex number in Python using the \b metacharacter. This metacharacter matches the beginning or end of a word. The following example code will match a hex number with up to 8 digits:

import re

hex_regex = re.compile(r'\b[0-9A-F]{1,8}\b')

hex_number = hex_regex.search('The hex number is 0xFFF')

print(hex_number.group())

Output example

0xFFF

Code explanation

  • import re: imports the re module which contains the re.compile() function used to create a regex object.
  • hex_regex = re.compile(r'\b[0-9A-F]{1,8}\b'): creates a regex object using the re.compile() function. The regex pattern \b[0-9A-F]{1,8}\b matches a hex number with up to 8 digits.
  • hex_number = hex_regex.search('The hex number is 0xFFF'): searches for a hex number in the given string using the regex.search() function.
  • print(hex_number.group()): prints the matched hex number using the group() method.

Helpful links

Edit this code on GitHub