Go Standard Library Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Open the console and create the folder chapter02/recipe07.
  2. Navigate to the directory.
  3. Create the regexp.go file with the following content:
        package main

import (
"fmt"
"regexp"
)

const refString = `[{ \"email\": \"email@example.com\" \
"phone\": 555467890},
{ \"email\": \"other@domain.com\" \
"phone\": 555467890}]`

func main() {

// This pattern is simplified for brevity
emailRegexp := regexp.MustCompile("[a-zA-Z0-9]{1,}
@[a-zA-Z0-9]{1,}\\.[a-z]{1,}")
first := emailRegexp.FindString(refString)
fmt.Println("First: ")
fmt.Println(first)

all := emailRegexp.FindAllString(refString, -1)
fmt.Println("All: ")
for _, val := range all {
fmt.Println(val)
}

}
  1. Run the code by executing the go run regexp.go.
  2. See the output in the Terminal: