You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
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 := `\d+`
|
|
|
|
regexComp := regex.MustCompile(regexStr)
|
|
|
|
|
|
|
|
matchStr := regexComp.FindString("The year of our lord, 2025")
|
|
|
|
fmt.Println(matchStr)
|
|
|
|
// Output: 2025
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|