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 String | Match Output |
---|---|
1XD2BcascMKHJKMLJuijknasdlascAJSCKa | does not match |
bc1qarsrrr7ASH3ydab9HJKMLJuijknasdah | matches |
FGXD2E$DRTFYGUHJNYUBHJKNM1HKNJMMKLJN | does not match |
3XD2t1ASDTaSDFSDQSADyysadasdassXasy | matches |
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. In conclusion, understanding and utilizing regular expressions (regex) to validate Bitcoin wallet addresses is crucial for secure and accurate Bitcoin transactions. By adhering to the specified conditions and using the provided regex pattern, developers and users can effectively verify the validity of Bitcoin addresses and ensure their funds’ safety. Regex provides a powerful tool for searching and manipulating text, making it an indispensable asset in programming, text processing, and data validation tasks.