SWIFT / BIC Code in GO
Validate SWIFT/BIC bank identifier codes — 8 chars (head office) or 11 chars (branch).
Try it in the GO tester →Pattern
regexGO
^[A-Z]{6}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]{6}[A-Z0-9]{2}(?:[A-Z0-9]{3})?$`)
input := `DEUTDEFF`
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
[A-Z]{6} matches the bank-and-country prefix. [A-Z0-9]{2} matches the location code. (?:[A-Z0-9]{3})? optionally matches the 3-character branch code, allowing both 8-char head-office BICs and 11-char branch BICs.
Examples
Input
DEUTDEFFMatches
DEUTDEFF
Input
BNPAFRPPXXXMatches
BNPAFRPPXXX
Input
INVALID1No match
—