上QQ阅读APP看书,第一时间看更新
How to do it...
- Open the console and create the folder chapter02/recipe07.
- Navigate to the directory.
- 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)
}
}
- Run the code by executing the go run regexp.go.
- See the output in the Terminal: