DevKits

Numbers

Match a hexadecimal number

Match hex literals like 0xFF or #FFAA00.

Pattern

Regular expression

0[xX][0-9A-Fa-f]+\b

How it works

The `0x` or `0X` prefix followed by one or more hex digits. Word boundary prevents partial matches.

Matches

  • 0xff
  • 0X1A2B
  • 0xdeadbeef

Non-matches

  • FF (no prefix)
  • 0y1F (bad prefix)

Language snippets

JavaScript

const re = /0[xX][0-9A-Fa-f]+\b/g;

Python

re.findall(r"0[xX][0-9A-Fa-f]+\b", text)

Go

re := regexp.MustCompile(`0[xX][0-9A-Fa-f]+\b`)

Java

Pattern.compile("0[xX][0-9A-Fa-f]+\\b")

Related patterns

Try any pattern live in the Regex Tester