Finance
Bitcoin address shape Regex
Bitcoin addresses come in two main families. Legacy and script addresses start with 1 or 3 and use the Base58 alphabet, which leaves out the easily confused characters 0, O, I and l. Newer SegWit and Taproot addresses start with bc1 and use the lowercase Bech32 alphabet. This pattern accepts both families with their allowed character sets and approximate lengths. It is helpful for wallet input fields, donation forms and log scanners. It does not decode the address or verify the built-in checksum, so a single wrong character can still match and send funds nowhere.
Pattern (JavaScript)
/^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$/Starts at the start of the line/string, then (one of the characters 13, then one of the characters a-km-zA-HJ-NP-Z1-9, between 25 and 34 times OR the text "bc1", then one of the characters ac-hj-np-z02-9, between 11 and 71 times), then the end of the line/string.
Limit: Checks the shape only; it does not verify the Base58Check or Bech32 checksum.
Examples
Matches
- ✓ 1TestAddrXyZ9abcdEFGHjkmnpqrs
- ✓ 3FakeSampWa11etShapeQ7zRkmnop
- ✓ bc1qtestq8x7xz5wq3f9g7d2c4v6n8m0p2r4t6y8
Does not match
- ✗ 0TestAddrXyZ9abcdEFGHjkmnpqrs
- ✗ 1Il0OinvalidCharsAddressShapeXX
- ✗ bc1qbio
- ✗ hello world
Try it
0 matches (JavaScript engine)
1TestAddrXyZ9abcdEFGHjkmnpqrs
3FakeSampWa11etShapeQ7zRkmnop
bc1qtestq8x7xz5wq3f9g7d2c4v6n8m0p2r4t6y8
0TestAddrXyZ9abcdEFGHjkmnpqrs
1Il0OinvalidCharsAddressShapeXX
bc1qbio
hello worldCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$/g;
const text = "1TestAddrXyZ9abcdEFGHjkmnpqrs";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$')
text = '1TestAddrXyZ9abcdEFGHjkmnpqrs'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$/';
$text = '1TestAddrXyZ9abcdEFGHjkmnpqrs';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$");
String text = "1TestAddrXyZ9abcdEFGHjkmnpqrs";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$`)
text := "1TestAddrXyZ9abcdEFGHjkmnpqrs"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$");
var text = "1TestAddrXyZ9abcdEFGHjkmnpqrs";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$").unwrap();
let text = "1TestAddrXyZ9abcdEFGHjkmnpqrs";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[ac-hj-np-z02-9]{11,71})$/
text = '1TestAddrXyZ9abcdEFGHjkmnpqrs'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are 0, O, I and l missing from the character class?
- Base58 drops those four characters because they look alike in many fonts. Excluding them means an address containing one of them cannot be a valid legacy Bitcoin address, so the pattern rejects it.
- Does this pattern match testnet addresses?
- No. Testnet addresses start with m, n, 2 or tb1. Add those prefixes as extra alternatives if your application needs to accept test network addresses.
- Is a matching address safe to send funds to?
- Not necessarily. Both address formats include a checksum that only a proper decoder can verify. Always run a Bitcoin library check before you sign or broadcast a transaction.
Related patterns
- Ethereum address shape^0x[a-fA-F0-9]{40}$
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$
- Common ISO 4217 currency code^(?:USD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|CNY|HKD|SGD|INR|AED|SAR|SEK|NOK|DKK|ZAR|BRL|MXN)$