Go (RE2)

Bitcoin Address in GO

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

Try it in the GO tester →

Pattern

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

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\b(?:[13][a-km-zA-HJ-NP-Z1-9]{25,34}|bc1[a-zA-HJ-NP-Z0-9]{25,62})\b`)
	input := `1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

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)