Validation
Hexadecimal String Regex
Hashes, byte dumps, memory addresses and encoded keys are written in hexadecimal. This pattern allows an optional 0x prefix, as used in many programming languages, followed by one or more digits from 0 to 9 and letters from a to f in either case. Anything outside that set, such as the letter g or a hash sign, makes the input fail. It does not check a fixed length, so if you are validating a specific value such as a SHA-256 hash, replace the plus with an exact count like {64}. For byte data, an even-length rule can be added with ([0-9a-fA-F]{2})+.
Pattern (JavaScript)
/^(0x)?[0-9a-fA-F]+$/Starts at the start of the line/string, then a group (the text "0x"), optionally (zero or one time), then a hexadecimal digit, one or more times, then the end of the line/string.
Examples
Matches
- ✓ deadBEEF
- ✓ 0x1f
- ✓ 0123456789abcdef
Does not match
- ✗ 0xZZ
- ✗ #ffffff
- ✗ 12g4
Try it
0 matches (JavaScript engine)
deadBEEF
0x1f
0123456789abcdef
0xZZ
#ffffff
12g4Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(0x)?[0-9a-fA-F]+$/g;
const text = "deadBEEF";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(0x)?[0-9a-fA-F]+$')
text = 'deadBEEF'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(0x)?[0-9a-fA-F]+$/';
$text = 'deadBEEF';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(0x)?[0-9a-fA-F]+$");
String text = "deadBEEF";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(0x)?[0-9a-fA-F]+$`)
text := "deadBEEF"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(0x)?[0-9a-fA-F]+$");
var text = "deadBEEF";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(0x)?[0-9a-fA-F]+$").unwrap();
let text = "deadBEEF";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(0x)?[0-9a-fA-F]+$/
text = 'deadBEEF'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I require a specific length like a SHA-256 hash?
- Use ^[0-9a-f]{64}$ for a lowercase SHA-256 digest. Each byte is two hex digits, so 32 bytes become 64 characters.
- Why is #ffffff rejected?
- The hash sign is not a hex digit. Colour codes have their own pattern with a hash prefix and fixed lengths.
- How do I make sure the string has whole bytes?
- Use ^([0-9a-fA-F]{2})+$. It accepts only an even number of hex digits.