Python (re)

Bitcoin Address in PY

Match Bitcoin addresses in legacy (P2PKH/P2SH) and SegWit Bech32 (bc1) formats.

Try it in the PY tester →

Pattern

regexPY
\b(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{25,62})\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\b(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{25,62})\b")
input_text = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Legacy addresses start with 1 or 3 followed by 25–34 Base58 characters (no 0, O, I, l). Bech32 SegWit addresses start with bc1 followed by 25–62 chars from its restricted alphabet.

Examples

Input

1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

Matches

  • 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

Input

3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy

Matches

  • 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy

Same pattern, other engines

← Back to Bitcoin Address overview (all engines)