diff --git a/regex/example_test.go b/regex/example_test.go index 8499dfc..d652ca7 100644 --- a/regex/example_test.go +++ b/regex/example_test.go @@ -53,6 +53,59 @@ func ExampleReg_FindSubmatch() { // 2 3 } +func ExampleReg_FindAllSubmatch() { + regexStr := `(\d)\.(\d)(\d)` + regexComp := regex.MustCompile(regexStr) + + matches := regexComp.FindAllSubmatch("3.14+8.97") + fmt.Println(matches[0][0]) // 0-group (entire match) of 1st match (0-indexed) + fmt.Println(matches[0][1]) // 1st group of 1st match + fmt.Println(matches[1][0]) // 0-group of 2nd match + fmt.Println(matches[1][1]) // 1st group of 2nd math + // Output: 0 4 + // 0 1 + // 5 9 + // 5 6 +} + +func ExampleReg_FindAllString() { + regexStr := `<0-255>\.<0-255>\.<0-255>\.<0-255>` + inputStr := `192.168.220.7 pings 9.9.9.9` + regexComp := regex.MustCompile(regexStr) + + matchStrs := regexComp.FindAllString(inputStr) + + fmt.Println(matchStrs[0]) + fmt.Println(matchStrs[1]) + // Output: 192.168.220.7 + // 9.9.9.9 +} + +func ExampleReg_FindAllStringSubmatch() { + // 'https' ... + // followed by 1 or more alphanumeric characters (including period) ... + // then a forward slash ... + // followed by one more of : + // word character, + // question mark, + // period, + // equals sign + regexStr := `https://([a-z0-9\.]+)/([\w.?=]+)` + regexComp := regex.MustCompile(regexStr, regex.RE_CASE_INSENSITIVE) + inputStr := `You can find me at https://twomorecents.org/index.html and https://news.ycombinator.com/user?id=aadhavans` + + matchIndices := regexComp.FindAllStringSubmatch(inputStr) + fmt.Println(matchIndices[0][1]) // 1st group of 1st match (0-indexed) + fmt.Println(matchIndices[0][2]) // 2nd group of 1st match + fmt.Println(matchIndices[1][1]) // 1st group of 2nd match + fmt.Println(matchIndices[1][2]) // 2nd group of 2nd match + // Output: twomorecents.org + // index.html + // news.ycombinator.com + // user?id=aadhavans + +} + func ExampleReg_Expand() { inputStr := `option1: value1 option2: value2`