Utilo

Hexadecimal Regular Expression – Free, Online, Private

Matches a hex string with an optional 0x prefix.

Tester
Sample
Matches (2)
  • 0xFF
  • deadbeef

The pattern

The pattern matches 0xFF and deadbeef. It does not match a bare 0x, because at least one hex digit is required after the optional prefix. zz is not hex. #fff is a color, and the hash is not a prefix this pattern allows. The i flag makes the prefix and the digits case-insensitive. The m flag tests the sample line by line.

The source is ^(?:0x)?[0-9a-fA-F]+$. The flags on this page are im. i means case-insensitive. m means ^ and $ match at line breaks, which is how the sample can show several candidates. g is not set. The tester finds non-overlapping matches, and without g a pattern with no anchor still reports the matches it walks. The box under the title is a tester with this pattern and the sample already filled in. Edit either one. Nothing you type is uploaded. The same tester, without a preset, is the regex tool.

How to read it

(?:0x)? is the optional prefix. Because of the i flag, 0X is accepted too. [0-9a-fA-F]+ is one or more hex digits. The character class does not include a sign. Negative hex is not a thing in this pattern. Anchors keep the match from succeeding on a prefix of a longer garbage string. If you are finding hex inside a log, drop the anchors and add a boundary.

What it will not do

The pattern does not limit the length. A hash, a color, and an address can all be hex. If you meant a color, use the hex-color pattern, which requires a hash and a CSS length. If you meant a SHA-256 digest, require exactly 64 digits and no prefix. The optional prefix means a validator cannot tell 'the user forgot the digits' from 'the user wrote 0'. 0 is valid hex. 0x is not.

A match is not a parsed value. An email pattern does not send mail, a UUID pattern does not allocate an id, and a password pattern does not store a password. When the next step needs a real parser, use one: the URL constructor, a date library, a semver library, or the converters on this site. The expression is the part you can see.

JavaScript, Python, and Go

The JavaScript snippet uses the RegExp constructor so the source does not have to be escaped for a literal slash. The Python snippet uses a normal string with the same escapes JSON would emit, which keeps \d as a regex digit class. Go's snippet uses a raw string. If the pattern contains a lookahead, the Go snippet says so and does not call MustCompile, because RE2 would reject it at runtime.

JavaScriptconst re = new RegExp("^(?:0x)?[0-9a-fA-F]+$", "im");
const found = text.match(re);
console.log(found ? found[0] : "no match");
Pythonimport re

pattern = re.compile("^(?:0x)?[0-9a-fA-F]+$", re.IGNORECASE | re.MULTILINE)
found = pattern.search(text)
print(found.group(0) if found else "no match")
Gopackage main

import (
	"fmt"
	"regexp"
)

func main() {
	text := ""
	re := regexp.MustCompile(`(?i)(?m)^(?:0x)?[0-9a-fA-F]+$`)
	fmt.Println(re.FindString(text))
}

Frequently asked questions

Does 0x match by itself?

No. The prefix is optional, but one hex digit is required.

Is #fff hex here?

No. The hash prefix belongs to the color pattern, not this one.

Are upper-case digits allowed?

Yes. The i flag and the class accept A-F and a-f.

Can I require a fixed length?

Replace the + with a count, for example {64} for a SHA-256 hex digest, and decide whether the 0x prefix is still optional.