Finance
Ethereum address shape Regex
An Ethereum address is 20 bytes written as 40 hexadecimal characters after a 0x prefix. The same format is used by most EVM-compatible chains, so this pattern also fits addresses on networks built on the Ethereum virtual machine. It is a fast check for wallet fields, smart contract configuration files and transaction log parsers. Mixed case is accepted because the EIP-55 checksum encodes itself in the capitalisation, but this pattern does not evaluate that checksum. An address with one mistyped character will still match, so confirm it with a library before sending anything.
Pattern (JavaScript)
/^0x[a-fA-F0-9]{40}$/Starts at the start of the line/string, then the text "0x", then one of the characters a-fA-F0-9, exactly 40 times, then the end of the line/string.
Limit: Checks the shape only; it does not verify the EIP-55 mixed-case checksum.
Examples
Matches
- ✓ 0x1234567890abcdef1234567890abcdef12345678
- ✓ 0xDEADBEEF00000000000000000000000000000000
- ✓ 0xAbCdEf0123456789aBcDeF0123456789AbCdEf01
Does not match
- ✗ 1234567890abcdef1234567890abcdef12345678
- ✗ 0x1234567890abcdef1234567890abcdef1234567
- ✗ 0x1234567890abcdef1234567890abcdef1234567g
- ✗ 0X1234567890abcdef1234567890abcdef12345678
Try it
0 matches (JavaScript engine)
0x1234567890abcdef1234567890abcdef12345678
0xDEADBEEF00000000000000000000000000000000
0xAbCdEf0123456789aBcDeF0123456789AbCdEf01
1234567890abcdef1234567890abcdef12345678
0x1234567890abcdef1234567890abcdef1234567
0x1234567890abcdef1234567890abcdef1234567g
0X1234567890abcdef1234567890abcdef12345678Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^0x[a-fA-F0-9]{40}$/g;
const text = "0x1234567890abcdef1234567890abcdef12345678";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^0x[a-fA-F0-9]{40}$')
text = '0x1234567890abcdef1234567890abcdef12345678'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^0x[a-fA-F0-9]{40}$/';
$text = '0x1234567890abcdef1234567890abcdef12345678';
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[a-fA-F0-9]{40}$");
String text = "0x1234567890abcdef1234567890abcdef12345678";
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[a-fA-F0-9]{40}$`)
text := "0x1234567890abcdef1234567890abcdef12345678"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^0x[a-fA-F0-9]{40}$");
var text = "0x1234567890abcdef1234567890abcdef12345678";
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[a-fA-F0-9]{40}$").unwrap();
let text = "0x1234567890abcdef1234567890abcdef12345678";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^0x[a-fA-F0-9]{40}$/
text = '0x1234567890abcdef1234567890abcdef12345678'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- What is the EIP-55 checksum?
- EIP-55 encodes a checksum by capitalising certain letters of the address based on its hash. Wallets use it to catch typos, but checking it requires computing Keccak-256, which a regex cannot do.
- Does this match ENS names like name.eth?
- No. ENS names are human-readable aliases that resolve to an address through a lookup. This pattern only matches the raw hexadecimal address format.
- Why is an uppercase 0X prefix rejected?
- Ethereum tooling consistently writes the prefix as lowercase 0x, so the pattern requires that form. If you receive uppercase prefixes from a legacy system, normalise them before testing.
Related patterns
- Bitcoin address shape^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$
- Common ISO 4217 currency code^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$