package main import ( "fmt" "regexp" ) func main() { // 1. 根据字符串匹配 m1, _ := regexp.MatchString("foo.*", "seafood") fmt.Println(m1) // true m2, _ := regexp.MatchString("^foo.*", "seafood") fmt.Println(m2) // false // 2. 根据字符串查找 reg1 := regexp.MustCompile(`\d{11}`) ret1 := reg1.FindString("15313067271886595446515153130672712") // 只查找第一个 ret2 := reg1.FindAllString("15313067271886545986715313067271", -1) // n:-1表示查找所有, 1找一个,2找2个 fmt.Println(ret1) // 15313067271 fmt.Println(ret2) // [15313067271 88654598671] // 3. 获取分组信息 reg2 := regexp.MustCompile(`(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)`) ret3 := reg2.FindStringSubmatch("dsf130429191912015219k13042919591219521Xkk") ret4 := reg2.FindAllStringSubmatch("dsf130429191912015219k13042919591219521Xkk", -1) fmt.Println(ret3) // [130429191912015219 130429 1919 12 01 521 9] fmt.Println(ret4) // [[130429191912015219 130429 1919 12 01 521 9] [13042919591219521X 130429 1959 12 19 521 X]] }