Ethereum Address Validation Using Regular Expressions
Last Updated :
09 Jan, 2024
ETHEREUM address is a 40-character hexadecimal identifier that receives and sends Ether (ETH) and other Ethereum-based tokens. Ethereum addresses come in two main formats: the standard hexadecimal representation and the checksummed format.
Examples:
Input: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e
Output: True
Explanation: This is a valid Ethereum address. It starts with “0x” and consists of 40 hexadecimal characters.
Input: 0x742d35cc6634c0532925a3b844bc454e4438f44e
Output: True
Explanation: This address is valid because it follows the correct format with “0x” at the beginning.
Input: 742d35Cc6634C0532925a3b844Bc454e4438f44e
Output: True
Explanation: Ethereum addresses are usually represented with “0x” at the start, but it’s not strictly required.
This address is valid because it consists of 40 hexadecimal characters, and the absence of “0x” doesn’t make it invalid.
Input: 0x123
Output: False
Explanation: This address is not valid because it lacks the required 40-character length.
Ethereum addresses must contain 40 hexadecimal characters after “0x.”
Input: 0123456789012345678901234567890
Output: True
Explanation: This address is valid because it contains 40 hexadecimal characters following “0x,”.
Correct Standard Hexadecimal Format
- Consists of 40 hexadecimal characters (0-9 and a-f).
- The “0x” prefix is optional but often included to denote that it’s a hexadecimal value.
- It only contains the characters 0-9 and a-f (lowercase).
- There are no spaces or other special characters in the address.
- It does not use the letters O, I, l, or the number 0 to avoid visual ambiguity.
Correct Checksummed Format (Mixed Case)
- Consists of 40 hexadecimal characters (0-9 and A-F or a-f).
- The “0x” prefix is optional but commonly used to denote that it’s a hexadecimal value.
- Characters in the address can be either uppercase (A-F) or lowercase (a-f).
- This mixed-case format is designed to provide more specificity and reduce the likelihood of typographical errors.
- There are no spaces or special characters in the address.
- It does not use the letters O, I, l, or the number 0 to avoid visual ambiguity.
Approach
- This problem can be solved with the help of Regular Expressions.
- Accept the Ethereum Address field as a string.
- Use the above regex pattern to validate the string.
- If the entered string will match the below-used regex then It will be a Valid Ethereum Address.
- If the entered string will not match with the below-written regex then entered string will be an invalid Ethereum Address.
Ethereum Address Regex Validation
Below is the regular expression (regex) pattern to validate Ethereum addresses in their standard hexadecimal format:
Regex:
regex : ^(0x)?[0-9a-fA-F]{40}$
Where,
^ and $: These anchors ensure that the entire string matches the pattern from start to finish.
(0x)?: The address may start with “0x” (optional) to denote it’s in hexadecimal format.
[0-9a-fA-F]{40}: This part matches exactly 40 characters, which should be hexadecimal characters (0-9 and A-F or a-f).
Below is the code implementation for the same:
C++
#include <iostream>
#include <regex>
class EthereumAddressValidator {
public :
static bool
isValidETHAddress( const std::string& address)
{
std::regex regexPattern( "^(0x)?[0-9a-fA-F]{40}$" );
return std::regex_match(address, regexPattern);
}
static std::string printResult( bool valid)
{
return valid ? "True" : "False" ;
}
};
int main()
{
std::string address1
= "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
std::cout << EthereumAddressValidator::printResult(
EthereumAddressValidator::isValidETHAddress(
address1))
<< std::endl;
std::string address3 = "742d35Cc6634C0532925a3b844Bc454"
"e4438f44e" ;
std::cout << EthereumAddressValidator::printResult(
EthereumAddressValidator::isValidETHAddress(
address3))
<< std::endl;
std::string address4 = "0x123" ;
std::cout << EthereumAddressValidator::printResult(
EthereumAddressValidator::isValidETHAddress(
address4))
<< std::endl;
std::string address5 = "0x12345678901234567890123456789"
"01234567890" ;
std::cout << EthereumAddressValidator::printResult(
EthereumAddressValidator::isValidETHAddress(
address5))
<< std::endl;
std::string address6
= "0xabc123XYZ" ;
std::cout << EthereumAddressValidator::printResult(
EthereumAddressValidator::isValidETHAddress(
address6))
<< std::endl;
return 0;
}
|
C
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
bool isValidETHAddress( const char * address)
{
regex_t regex;
int reti;
const char * regexPattern = "^(0x)?[0-9a-fA-F]{40}$" ;
reti = regcomp(®ex, regexPattern, REG_EXTENDED);
if (reti) {
fprintf (stderr, "Could not compile regex\n" );
return false ;
}
reti = regexec(®ex, address, 0, NULL, 0);
regfree(®ex);
if (!reti) {
return true ;
}
else if (reti == REG_NOMATCH) {
return false ;
}
else {
char errorBuffer[100];
regerror(reti, ®ex, errorBuffer,
sizeof (errorBuffer));
fprintf (stderr, "Regex match failed: %s\n" ,
errorBuffer);
return false ;
}
}
void printResult( bool valid)
{
if (valid) {
printf ( "True\n" );
}
else {
printf ( "False\n" );
}
}
int main()
{
const char * address1
= "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
printResult(isValidETHAddress(address1));
const char * address2
= "0x742d35cc6634c0532925a3b844bc454e4438f44e" ;
printResult(isValidETHAddress(address2));
const char * address3 = "742d35Cc6634C0532925a3b844Bc454"
"e4438f44e" ;
printResult(isValidETHAddress(address3));
const char * address4 = "0x123" ;
printResult(isValidETHAddress(address4));
const char * address5 = "0x12345678901234567890123456789"
"01234567890" ;
printResult(isValidETHAddress(address5));
const char * address6
= "0xabc123XYZ" ;
printResult(isValidETHAddress(address6));
return 0;
}
|
Java
import java.util.regex.*;
class EthereumAddressValidator {
public static boolean isValidETHAddress(String address)
{
String regex = "^(0x)?[0-9a-fA-F]{40}$" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(address);
return matcher.matches();
}
public static String print( boolean valid)
{
return valid ? "True" : "False" ;
}
public static void main(String[] args)
{
String address1
= "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
System.out.println(
print(isValidETHAddress(address1)));
String address2
= "0x742d35cc6634c0532925a3b844bc454e4438f44e" ;
System.out.println(
print(isValidETHAddress(address2)));
String address3
= "742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
System.out.println(
print(isValidETHAddress(address3)));
String address4 = "0x123" ;
System.out.println(
print(isValidETHAddress(address4)));
String address5
= "0x1234567890123456789012345678901234567890" ;
System.out.println(
print(isValidETHAddress(address5)));
String address6
= "0xabc123XYZ" ;
System.out.println(
print(isValidETHAddress(address6)));
}
}
|
Python3
import re
class EthereumAddressValidator:
@staticmethod
def is_valid_eth_address(address):
regex = r '^(0x)?[0-9a-fA-F]{40}$'
return bool (re.match(regex, address))
@staticmethod
def print_result(valid):
return "True" if valid else "False"
if __name__ = = "__main__" :
address1 = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
print (EthereumAddressValidator.print_result(
EthereumAddressValidator.is_valid_eth_address(address1)))
address2 = "0x742d35cc6634c0532925a3b844bc454e4438f44e"
print (EthereumAddressValidator.print_result(
EthereumAddressValidator.is_valid_eth_address(address2)))
address3 = "742d35Cc6634C0532925a3b844Bc454e4438f44e"
print (EthereumAddressValidator.print_result(
EthereumAddressValidator.is_valid_eth_address(address3)))
address4 = "0x123"
print (EthereumAddressValidator.print_result(
EthereumAddressValidator.is_valid_eth_address(address4)))
address5 = "0x1234567890123456789012345678901234567890"
print (EthereumAddressValidator.print_result(
EthereumAddressValidator.is_valid_eth_address(address5)))
address6 = "0xabc123XYZ"
print (EthereumAddressValidator.print_result(
EthereumAddressValidator.is_valid_eth_address(address6)))
|
C#
using System;
using System.Text.RegularExpressions;
class EthereumAddressValidator {
static bool IsValidETHAddress( string address)
{
string regexPattern = @"^(0x)?[0-9a-fA-F]{40}$" ;
Regex regex = new Regex(regexPattern);
return regex.IsMatch(address);
}
static string PrintResult( bool valid)
{
return valid ? "True" : "False" ;
}
static void Main()
{
string address1
= "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
Console.WriteLine(
PrintResult(IsValidETHAddress(address1)));
string address2
= "0x742d35cc6634c0532925a3b844bc454e4438f44e" ;
Console.WriteLine(
PrintResult(IsValidETHAddress(address2)));
string address3
= "742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
Console.WriteLine(
PrintResult(IsValidETHAddress(address3)));
string address4 = "0x123" ;
Console.WriteLine(
PrintResult(IsValidETHAddress(address4)));
string address5
= "0x1234567890123456789012345678901234567890" ;
Console.WriteLine(
PrintResult(IsValidETHAddress(address5)));
string address6
= "0xabc123XYZ" ;
Console.WriteLine(
PrintResult(IsValidETHAddress(address6)));
}
}
|
Javascript
function isValidETHAddress(str) {
let regex = new RegExp(/^(0x)?[0-9a-fA-F]{40}$/);
if (str == null ) {
return "False" ;
}
if (regex.test(str) == true ) {
return "True" ;
}
else {
return "False" ;
}
}
let address1 = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
console.log(isValidETHAddress(address1));
let address2 = "0x742d35cc6634c0532925a3b844bc454e4438f44e" ;
console.log(isValidETHAddress(address2));
let address3 = "742d35Cc6634C0532925a3b844Bc454e4438f44e" ;
console.log(isValidETHAddress(address3));
let address4 = "0x123" ;
console.log(isValidETHAddress(address4));
let address5 = "0x1234567890123456789012345678901234567890" ;
console.log(isValidETHAddress(address5));
let address6 = "0xabc123XYZ" ;
console.log(isValidETHAddress(address6));
|
Output
True
True
False
True
False
Time Complexity: O(N)
Auxiliary Space: O(1) or O(N), depending on the implementation of the regular expression library and the specific input data.
Share your thoughts in the comments
Please Login to comment...