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")

Frequently asked questions

What is the regex for hexadecimal number?

The pattern 0[xX][0-9A-Fa-f]+\b matches hexadecimal number. The `0x` or `0X` prefix followed by one or more hex digits. Word boundary prevents partial matches.

What does this hexadecimal number pattern match?

It matches strings like 0xff, 0X1A2B, 0xdeadbeef, but rejects FF (no prefix), 0y1F (bad prefix).

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester