Regex
regex
December 27, 20223 min read

Regex for Bitcoin Wallet Address validation

A Bitcoin address is a unique identifier that is used to send and receive Bitcoin transactions. It is a string of letters and numbers that starts with the number "1" or "3". Here is an example of a Bitcoin address- 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2. In this article let's understand how we can create a regex for a Bitcoin Address and how regex can be matched for a Bitcoin Address.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Conditions to match Bitcoin Wallet Address

  • A BTC address is an identifier containing 26-35 alphanumeric characters.
  • A BTC address starts with the numbers 1, 3, or bc1.
  • It contains digits in the range of 0 to 9.
  • It allows uppercase as well as lowercase alphabet characters.
  • There is one exceptional point to be noted: The uppercase letter O, the uppercase letter I, the lowercase letter l, and the number 0 are not used to avoid visual ambiguity.
  • It should not contain whitespaces and other special characters.

Regex for checking if Bitcoin Wallet Address is valid or not

Regular Expression-

/^(bc1|[13])[a-km-zA-HJ-NP-Z1-9]{25,34}$/gm

Test string examples for the above regex-

Input StringMatch Output
1XD2BcascMKHJKMLJuijknasdlascAJSCKadoes not match
bc1qarsrrr7ASH3ydab9HJKMLJuijknasdahmatches
FGXD2E$DRTFYGUHJNYUBHJKNM1HKNJMMKLJNdoes not match
3XD2t1ASDTaSDFSDQSADyysadasdassXasymatches

Here is a detailed explanation of the above regex-

/^(bc1|[13])[a-km-zA-HJ-NP-Z1-9]{25,34}$/gm

^ asserts position at start of a line
1st Capturing Group (bc1|[13])
1st Alternative bc1
bc1 matches the characters bc1 literally (case sensitive)
2nd Alternative [13]
Match a single character present in the list below [13]
13 matches a single character in the list 13 (case sensitive)
Match a single character present in the list below [a-km-zA-HJ-NP-Z1-9]
{25,34} matches the previous token between 25 and 34 times, as many times as possible, giving back as needed (greedy)
a-k matches a single character in the range between a (index 97) and k (index 107) (case sensitive)
m-z matches a single character in the range between m (index 109) and z (index 122) (case sensitive)
A-H matches a single character in the range between A (index 65) and H (index 72) (case sensitive)
J-N matches a single character in the range between J (index 74) and N (index 78) (case sensitive)
P-Z matches a single character in the range between P (index 80) and Z (index 90) (case sensitive)
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if a bitcoin wallet address is valid or not.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 27, 20222 min read
Next Article