Regex for Ethereum Addresses

With the increased use of Ethereum addresses these days, it’s important to have a way to check that a user-generated Ethereum address is valid.

So what is the best regular expression for matching Ethereum address?

A regular expression for matching Ethereum addresses must check for a leading 0xEdit with Regexity followed by a random string of 40 hexadecimal characters (lowercase a-fEdit with Regexity, uppercase A-FEdit with Regexity, and numbers 0-9Edit with Regexity). These expressions are not case sensitive, although a capitalized checksum version exists that refers to the same account but provides an added layer of security.

The regex code therefore looks something like this:

/^0x[a-fA-F0-9]{40}$/Edit with Regexity

Let’s break that down to see how it’s made up.

General Format of Ethereum Addresses

An Ethereum address is valid if it satisfies the following criteria:

Ethereum addresses are case insensitive. They work perfectly in lowercase, but capitalized checksum versions exist for every address which refers to the same Ethereum account but provides an additional check to ensure the address is valid and does not contain any copying errors. More about this at the end of this post.

Regular Expression for Ethereum Addresses

The regex code starts with the literal characters 0xEdit with Regexity since these are present at the leading end of any Ethereum address.

/0x/Edit with Regexity

ALSO READ: Regex for Bitcoin Addresses

After that, the address can contain 40 random hexadecimal characters, either uppercase or lowercase. These characters are described by adding all possible characters in square brackets. The square brackets indicate that the software can match any occurrence of those characters.

/0x[a-fA-F0-9]/Edit with Regexity

Finally, we only want 40 hexadecimal characters, so we add a quantifier in curly brackets at the end:

/0x[a-fA-F0-9]{40}/Edit with Regexity

Finally, we need to ensure that we match nothing else before or after these characters. Thus we need to add a start-of-string ^Edit with Regexity and end-of-string $Edit with Regexity character to the front and back.

/^0x[a-fA-F0-9]{40}$/Edit with Regexity

That’s the final expression! It should be able to validate any Ethereum address.

Scraping Ethereum Addresses

If you’d like to scrape all the Ethereum address on a page, you can remove the start-of-string and end-of-string characters from the preceding regex. This will ensure it matches multiple instances of the address throughout a larger body of text.

/0x[a-fA-F0-9]{40}/Edit with Regexity

Which Flags to Use

Also, include the global gEdit with Regexity flag at the end. This will ensure that all the instances are matched. If you leave out the global flag, only the first address will be matched.

/0x[a-fA-F0-9]{40}/gEdit with Regexity

There is no need to include the case insensitive flag iEdit with Regexity since Ethereum addresses work both in lowercase or in the capitalized checksum versions. Both versions refer to the same account.

Other Considerations

This regex expression will only validate the format of an Ethereum address. However, valid Ethereum addresses can also be checked by means of a capitalization checksum as explained here. This will need to be checked by a programmable language such as Javascript of Python.

Sources

The regular expressions on this page were adapted from solutions posted on Stack Exchange by Jesbus on this question, by Stepan Poperechnyi on this question. In addition, this solution and this Reddit thread also proved helpful.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

Leave a Comment