正則匹配而且能夠捕獲到()這個裏面的子表達式的值,linux的grep命令沒辦法捕獲子表達式的值,只能獲取到整條正則匹配的內容html
package main import "regexp" import "fmt" func main() { str := `(.*?)(\d+)(.*?)\d(.*)\d` r := regexp.MustCompile(str) matchs := r.FindStringSubmatch("tao123shi5han567") for _, s := range matchs { fmt.Println(s) } }
上面的正則中驗證了.*是貪婪 .*?是非貪婪 ,下面匹配的字符串切片第一條是整條數據,後面的每個對應正則括號裏捕獲的內容linux
tao@tao-PC:/var/www/html/go-project/test$ go run test.go
tao123shi5han567
tao
123
shi
han56spa