Go (RE2)

US Social Security Number in GO

Match US Social Security Numbers in the canonical XXX-XX-XXXX format.

Try it in the GO tester →

Pattern

regexGO
\b\d{3}-\d{2}-\d{4}\b   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\b\d{3}-\d{2}-\d{4}\b`)
	input := `123-45-6789`
	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

Three digits, hyphen, two digits, hyphen, four digits. Word boundaries prevent false matches inside longer digit runs. Note: this pattern does not validate SSN issuance rules.

Examples

Input

123-45-6789

Matches

  • 123-45-6789

Input

SSN: 987-65-4321 on file

Matches

  • 987-65-4321

Input

123456789

No match

Same pattern, other engines

← Back to US Social Security Number overview (all engines)