Skip to content

Instantly share code, notes, and snippets.

@MBrassey
Last active April 11, 2024 12:44
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save MBrassey/623f7b8d02766fa2d826bf9eca3fe005 to your computer and use it in GitHub Desktop.
Save MBrassey/623f7b8d02766fa2d826bf9eca3fe005 to your computer and use it in GitHub Desktop.
Match Crypto Wallet Addresses (REGEX)

Match Crypto Wallet Addresses using Regular Expressions

The following regular expressions are crafted to match some commonly used cryptocurrency wallet address types. This document details the Regex components and pattern tests to match Ethereum, Bitcoin, Dash and Monero addresses.

🌀 Click the images below to view the tested patterns.

Ethereum (ETH)

/^0x[a-fA-F0-9]{40}$/g

Bitcoin (BTC)

/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/g

Dash (DASH)

/X[1-9A-HJ-NP-Za-km-z]{33}$/g

Monero (XMR)

/4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}$/g

Table of Contents

Regex Components

Anchors

/

Open. Indicates the start of a regular expression.

^

Beginning. Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.

$

End. Matches the end of the string, or the end of a line if the multiline flag (m) is enabled.

/

Close. Second "/" indicates the end of a regular expression and the start of expression flags.

Quantifiers

{40}

Ethereum (ETH): Match 40 of the preceeding token.

{25,39}

Bitcoin (BTC): Match between 25 and 39 of the preceeding token.

{33}

Dash (DASH): Match between 33 of the preceeding token.

{93}

Monero (XMR): Match between 93 of the preceeding token.

OR Operator

|

Bitcoin (BTC): Alternation acts like a boolean OR. Matches the expression before or after the "|".

Character Classes

Ethereum (ETH):
0

Character. Matches a "0" character (char code 48).

x

Character. Matches a "x" character (char code 120). Case sensitive.

[a-fA-F0-9]

Character Set. Match any character in the set. a-f Range matches a character in the range "a" to "f" (char code 97 to 102). Case sensitive. A-F Range matches a character in the range "A" to "F" (char code 65 to 70). Case sensitive. 0-9 matches a character in the range of "0" to "9" (char code 48 to 57). Case sensitive.

Bitcoin (BTC):
[a-zA-HJ-NP-Z0-9]

Character set. Match any character in the set. a-z Range matches a character in the range "a" to "z" (char code 97 to 122). Case sensitive. A-H Range matches a character in the range "A" to "H" (char code 65 to 72). Case sensitive. J-N Range matches a character in the range "J" to "N" (char code 74 to 78). Case sensitive. P-Z Range matches a character in the range "P" to "Z" (char code 80 to 90). Case sensitive. 0-9 Range matches a character in the range "0" to "9" (char code 48 to 57). Case sensitive.

Dash (DASH):
X

Character. Matches a "X" character (char code 88). Case sensitive.

[1-9A-HJ-NP-Za-km-z]

Character set. Match hany character in the set. 1-9 Range matches a character in the range "1" to "9" (char code 49 to 57) Case sensitive. A-H Range matches a character in the range "A" to "H" (char code 65 to 72). J-N Range matches a character in the range "J" to "N" (char code 74 to 78). P-Z Range matches a character in the range "P" to "Z" (char code 80 to 90). Case sensitive. a-k Range matches a character in the range "a" to "k" (char code 97 to 107). Case sensitive. m-z Range matches a character in the range "m" to "z" (char code 109 to 122). Case sensitive.

Monero (XMR):
4

Character. Matches a "4" character (char code 52).

[0-9AB]

Character set. Match any character in the set. 0-9 Range matches a character in the range "0" to "9" (char code 48 to 57). Case sensitive. A Character matches "A" character (char code 65). Case sensitive. B Character matches a "B" character (char code 66). Case sensitive.

[1-9A-HJ-NP-Za-km-z]

Character set. Match any character in the set. 1-9 Range matches a character in the range "1" to "9" (char code 49 to 57) Case sensitive. A-H Range matches a character in the range "A" to "H" (char code 65 to 72). Case sensitive. J-N Range matches a character in the range "J" to "N" (char code 74 to 78). Case sensitive. P-Z Range matches a character in the range "P" to "Z" (char code 80 to 90). Case sensitive. a-k Range matches a character in the range "a" to "k" (char code 97 to 107). Case sensitive. m-z Range matches a character in the range "m" to "z" (char code 109 to 122). Case sensitive.

Flags

g

Global search. Retain the index of the last match, allowing subsequent searches to start from the end of the previous match. Without the global flag, subsequent searches will return the same match.

Grouping and Capturing

(bc1|[13])

Bitcoin (BTC): Capturing Group. Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference. b Character matches a "b" character (char code 98). Case sensitive. c Character matches a "c" character (char code 99). Case sensitive. 1 Character matches a "1" character (char code 49). | Alternation acts like a boolean OR. Matches the expression before or after the "|". Character set Matches any character in the set; 1 character matches a "1" character (char code 49), 3 character matches a "3" character (char code 51).

Author

I'm Matt Brassey - Full stack developer with a focus on decentralized applications. Visit my GitHub!

@hubciorz
Copy link

hubciorz commented Aug 2, 2022

Outdated for Monero (it does not cover subaddresses)

@guptabhaskar
Copy link

regex of btc address is different in the image

@cifelse
Copy link

cifelse commented Feb 18, 2023

How about for Solana addresses?

@martiscore
Copy link

Thank you. Using these Regex patterns, i can efficiently validate user input before processing transactions. Safeguarding users' assets and ensuring accurate fund transfers are essential in the ever-evolving crypto landscape.

Now, leverage this secure address validation to enhance your usdt to btc exchange platform. A reliable and validated Crypto Wallet Address system establishes trust among users, enhancing user experience, and elevating your exchange's reputation in the competitive market.

@jbelke
Copy link

jbelke commented Jul 17, 2023

Bitcoin:
^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$

@js-ms
Copy link

js-ms commented Jan 18, 2024

What about taproot support for the regex in bitcoin

@bullishgopher
Copy link

bullishgopher commented Feb 14, 2024

How about for Solana addresses?

new RegExp(/^[1-9A-HJ-NP-Za-km-z]{44}$/)

@cifelse

@cifelse
Copy link

cifelse commented Feb 14, 2024

Love that I was replied after a year. Thanks mate nonetheless @bullishgopher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment