package regex_test import ( "fmt" "gitea.twomorecents.org/Rockingcool/kleingrep/regex" ) func ExampleReg_Find() { regexStr := "b|a" regexComp := regex.MustCompile(regexStr) match, _ := regexComp.Find("banana") fmt.Println(match.String()) // Output: 0 1 } func ExampleReg_FindAll() { regexStr := "b|a" regexComp := regex.MustCompile(regexStr) matches := regexComp.FindAll("banana") for _, group := range matches { fmt.Println(group.String()) } // Output: 0 1 // 1 2 // 3 4 // 5 6 } func ExampleReg_FindString() { regexStr := `\w+\s+(?=sheep)` regexComp := regex.MustCompile(regexStr) matchStr := regexComp.FindString("pink cows and yellow sheep") fmt.Println(matchStr) // Output: yellow } func ExampleReg_FindSubmatch() { regexStr := `(\d)\.(\d)(\d)` regexComp := regex.MustCompile(regexStr) match, _ := regexComp.FindSubmatch("3.14") fmt.Println(match[0]) fmt.Println(match[1]) fmt.Println(match[2]) // Output: 0 4 // 0 1 // 2 3 } func ExampleReg_FindStringSubmatch() { regexStr := `(\d{4})-(\d{2})-(\d{2})` regexComp := regex.MustCompile(regexStr) inputStr := `The date is 2025-02-10` match := regexComp.FindStringSubmatch(inputStr) fmt.Println(match[1]) fmt.Println(match[3]) // Output: 2025 // 10 } 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` regexStr := `(\w+): (\w+)` templateStr := "$1 = $2\n" regexComp := regex.MustCompile(regexStr, regex.RE_MULTILINE) result := "" for _, submatches := range regexComp.FindAllSubmatch(inputStr) { result = regexComp.Expand(result, templateStr, inputStr, submatches) } fmt.Println(result) // Output: option1 = value1 // option2 = value2 } func ExampleReg_LiteralPrefix() { regexStr := `a(b|c)d*` regexComp := regex.MustCompile(regexStr) prefix, complete := regexComp.LiteralPrefix() fmt.Println(prefix) fmt.Println(complete) // Output: a // false } func ExampleReg_Longest() { regexStr := `x|xx` inputStr := "xx" regexComp := regex.MustCompile(regexStr) fmt.Println(regexComp.FindString(inputStr)) regexComp.Longest() fmt.Println(regexComp.FindString(inputStr)) // Output: x // xx }