Bitcoin Address (P2SH — starts with 3) Regex for Python
/^3[a-km-zA-HJ-NP-Z1-9]{25,34}$/What this pattern does
This page provides a well-structured, multi-part regular expression for matching bitcoin address (p2sh — starts with 3), ported and verified for Python. Financial data validation has zero tolerance for false negatives — a missed invalid entry can corrupt downstream calculations. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# Bitcoin Address (P2SH — starts with 3)
# ReDoS-safe | RegexVault — Finance > Crypto
import re
bitcoin_address_p2sh_starts_with_3_pattern = re.compile(r'^3[a-km-zA-HJ-NP-Z1-9]{25,34}$')
def validate_bitcoin_address_p2sh_starts_with_3(value: str) -> bool:
return bool(bitcoin_address_p2sh_starts_with_3_pattern.fullmatch(value))
# Example
print(validate_bitcoin_address_p2sh_starts_with_3("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy | 1A1zP1eP5QGefi2DMPTfTL5SLmv7Divf |
3Ai1JZ8pdJb2ksieUV8FsxSNVJCpoPi8W6 | bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq |
| — | 2J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy |
When to use this pattern
This pattern is drawn from the Finance > Crypto category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
A P2SH address alone does not reveal the underlying script (multisig, timelock, etc.). To verify a multisig arrangement, you need the redeem script, not just the address.
Technical Notes
P2SH addresses start with 3. Commonly used for multisignature wallets and wrapped SegWit (P2SH-P2WPKH). More flexible than P2PKH — can represent complex spending conditions.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern