Python (re)

Hexadecimal Number Literal in PY

Match hexadecimal number literals like `0xFF`, `0x1A2B`, or `0XdeadBeef`.

Try it in the PY tester →

Pattern

regexPY
\b0[xX][0-9a-fA-F]+\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\b0[xX][0-9a-fA-F]+\b")
input_text = "Mask = 0xFF; magic = 0xDEADBEEF;"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

\b is a word boundary so we don't match inside identifiers like `var0x10`. 0[xX] requires the literal prefix (case-insensitive on the x). [0-9a-fA-F]+ matches one or more hex digits in either case. Trailing \b avoids matching into adjacent letters.

Examples

Input

Mask = 0xFF; magic = 0xDEADBEEF;

Matches

  • 0xFF
  • 0xDEADBEEF

Input

Color #ff0000 vs literal 0xff0000

Matches

  • 0xff0000

Input

no hex here

No match

Same pattern, other engines

← Back to Hexadecimal Number Literal overview (all engines)