Go (RE2)

US Phone Number in GO

Match US phone numbers in common formats: (555) 867-5309, 555-867-5309, 5558675309.

Try it in the GO tester →

Pattern

regexGO
\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}`)
	input := `(555) 867-5309`
	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

The optional \(? and \)? allow parentheses around the area code. The [\s.\-]? between groups allows spaces, dots, or hyphens as optional separators.

Examples

Input

(555) 867-5309

Matches

  • (555) 867-5309

Input

555.867.5309

Matches

  • 555.867.5309

Input

5558675309

Matches

  • 5558675309

Same pattern, other engines

← Back to US Phone Number overview (all engines)