Numbersflags: g
Hexadecimal Number Literal
Match hexadecimal number literals like `0xFF`, `0x1A2B`, or `0XdeadBeef`.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("\\b0[xX][0-9a-fA-F]+\\b", "g");
const input = "Mask = 0xFF; magic = 0xDEADBEEF;";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
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+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b0[xX][0-9a-fA-F]+\b`)
input := `Mask = 0xFF; magic = 0xDEADBEEF;`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
regexengine-agnostic
\b0[xX][0-9a-fA-F]+\b (flags: g)Raw source: \b0[xX][0-9a-fA-F]+\b
How it 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
0xFF0xDEADBEEF
Input
Color #ff0000 vs literal 0xff0000Matches
0xff0000
Input
no hex hereNo match
—Common use cases
- •Source-code analysis for magic numbers
- •Memory-address parsing in stack traces
- •Color-format detection
- •Embedded / firmware tooling
Related patterns
Binary Number Literal
NumbersMatch binary number literals like `0b1010` or `0B11110000`.
Octal Number Literal
NumbersMatch modern ECMAScript-style octal literals (`0o755`) — strict per ES6+ syntax.
Decimal Number
NumbersMatches decimal numbers, including integers and negatives.
Float / Scientific Number
NumbersMatch floating-point and scientific-notation numbers including `1.5`, `.25`, `1e10`, `-3.14E-2`.