Regex for Hex String
Regex Pattern
^[0-9a-fA-F]+$Any-length hexadecimal string
Quick Answer
The regex pattern for hex string is `^[0-9a-fA-F]+$`. Any-length hexadecimal string. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| deadbeef | ✓ Matches |
| 0x1234 | ✓ Matches |
| FF00FF00 | ✓ Matches |
| 1a2b3c | ✓ Matches |
| ghij | ✗ No match |
| 0xGG | ✗ No match |
| not hex | ✗ No match |
Code Examples
javascript
const regex = /^[0-9a-fA-F]+$/; const isValid = regex.test(value);
python
import re
pattern = r'^[0-9a-fA-F]+$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[0-9a-fA-F]+$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^[0-9a-fA-F]+$/', $value)) {
echo "valid";
}java
String pattern = "^[0-9a-fA-F]+$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
UUID v4
UUID version 4 (randomly generated)
UUID (Any Version)
Any RFC 4122 UUID (versions 1-5)
JWT Token
JSON Web Token (header.payload.signature)
Base64 String
Standard base64 encoded string
Alphanumeric Username
Username with letters, digits, underscores (3-16 chars)
Twitter Handle
Twitter/X handle, 1-15 chars, optional @