DD
DevDash

Regex for Ethereum Address

Regex Pattern

^0x[0-9a-fA-F]{40}$

Ethereum address — 0x prefix followed by 40 hex characters

Quick Answer

The regex pattern for ethereum address is `^0x[0-9a-fA-F]{40}$`. Ethereum address — 0x prefix followed by 40 hex characters. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18✓ Matches
0x0000000000000000000000000000000000000000✓ Matches
742d35Cc6634C0532925a3b844Bc9e7595f2bD18✗ No match
0x742d35Cc✗ No match
0xGGGGGG✗ No match

Code Examples

javascript

const regex = /^0x[0-9a-fA-F]{40}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^0x[0-9a-fA-F]{40}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^0x[0-9a-fA-F]{40}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^0x[0-9a-fA-F]{40}$/', $value)) {
    echo "valid";
}

java

String pattern = "^0x[0-9a-fA-F]{40}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.