Ethereum Address Validation Using Regular Expressions

·

Ethereum addresses are essential for sending and receiving Ether (ETH) and other Ethereum-based tokens. These addresses come in two primary formats: standard hexadecimal and checksummed (mixed-case). Understanding how to validate them ensures secure transactions and avoids errors. Below, we explore Ethereum address validation using regular expressions (regex), covering formats, examples, and implementation.


Ethereum Address Formats

1. Standard Hexadecimal Format

2. Checksummed Format (Mixed Case)


Validation Examples

InputOutputExplanation
0x742d35Cc6634C0532925a3b844Bc454e4438f44eTrueValid 40-character address with "0x" prefix.
0x742d35cc6634c0532925a3b844bc454e4438f44eTrueValid lowercase address.
742d35Cc6634C0532925a3b844Bc454e4438f44eTrueValid without "0x" prefix.
0x123FalseInvalid: Too short (requires 40 chars).
0123456789012345678901234567890123456789TrueValid 40-character hexadecimal (no "0x").

Validation Approach

  1. Regex Pattern:

    ^(0x)?[0-9a-fA-F]{40}$
    • ^ and $: Match start-to-end.
    • (0x)?: Optional "0x" prefix.
    • [0-9a-fA-F]{40}: Exactly 40 hexadecimal chars.
  2. Steps:

    • Accept input as a string.
    • Match against the regex.
    • Return True if valid; False otherwise.

Code Implementation

import re

def validate_ethereum_address(address):
    pattern = r'^(0x)?[0-9a-fA-F]{40}$'
    return bool(re.fullmatch(pattern, address))

# Test Cases
print(validate_ethereum_address("0x742d35Cc6634C0532925a3b844Bc454e4438f44e"))  # True
print(validate_ethereum_address("742d35Cc6634C0532925a3b844Bc454e4438f44e"))   # True
print(validate_ethereum_address("0x123"))                                      # False
print(validate_ethereum_address("0123456789012345678901234567890123456789"))   # True

Output:

True
True
False
True
False

Complexity:


FAQs

1. Is "0x" mandatory in Ethereum addresses?

No. While commonly used, omitting "0x" doesn’t invalidate the address if the 40-character hex is correct.

2. Why use mixed-case (checksummed) addresses?

Mixed-case addresses reduce typo risks by incorporating case-sensitive checksums for validation.

3. Can regex validate checksummed addresses?

Basic regex checks format, but full checksum validation requires additional algorithmic steps (e.g., EIP-55).

👉 Learn more about Ethereum security


Key Takeaways

By mastering these techniques, you’ll ensure seamless and secure Ethereum transactions.

👉 Explore advanced blockchain tools