package main import ( "fmt" "regexp" ) func main() { // regular expression pattern regE := regexp.MustCompile("/oid/([\\d]+)/") // simulate a search // first convert string to byte for Find() function searchByte := []byte("/oid/1/") matchSlice := regE.Find(searchByte) fmt.Printf("%s \n", matchSlice) // if found, return leftmost match, without 'abc' matchSlice2 := regE.FindAll(searchByte, 500) fmt.Printf("%s \n", matchSlice2) // if found, return all successive matches oid := regE.NumSubexp() fmt.Printf("OID is %d \n", oid) // this is how to search by string matchSlice3 := regE.FindAllString(string(searchByte), -1) fmt.Printf("%s \n", matchSlice3) // if found, return all successive matches }
有疑问加站长微信联系(非本文作者)