| | |
| | |
| | |
| |
|
| | package regexp_test |
| |
|
| | import ( |
| | "fmt" |
| | "regexp" |
| | "strings" |
| | ) |
| |
|
| | func Example() { |
| | |
| | |
| | var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`) |
| |
|
| | fmt.Println(validID.MatchString("adam[23]")) |
| | fmt.Println(validID.MatchString("eve[7]")) |
| | fmt.Println(validID.MatchString("Job[48]")) |
| | fmt.Println(validID.MatchString("snakey")) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleMatch() { |
| | matched, err := regexp.Match(`foo.*`, []byte(`seafood`)) |
| | fmt.Println(matched, err) |
| | matched, err = regexp.Match(`bar.*`, []byte(`seafood`)) |
| | fmt.Println(matched, err) |
| | matched, err = regexp.Match(`a(b`, []byte(`seafood`)) |
| | fmt.Println(matched, err) |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleMatchString() { |
| | matched, err := regexp.MatchString(`foo.*`, "seafood") |
| | fmt.Println(matched, err) |
| | matched, err = regexp.MatchString(`bar.*`, "seafood") |
| | fmt.Println(matched, err) |
| | matched, err = regexp.MatchString(`a(b`, "seafood") |
| | fmt.Println(matched, err) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleQuoteMeta() { |
| | fmt.Println(regexp.QuoteMeta(`Escaping symbols like: .+*?()|[]{}^$`)) |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_Find() { |
| | re := regexp.MustCompile(`foo.?`) |
| | fmt.Printf("%q\n", re.Find([]byte(`seafood fool`))) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAll() { |
| | re := regexp.MustCompile(`foo.?`) |
| | fmt.Printf("%q\n", re.FindAll([]byte(`seafood fool`), -1)) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAllSubmatch() { |
| | re := regexp.MustCompile(`foo(.?)`) |
| | fmt.Printf("%q\n", re.FindAllSubmatch([]byte(`seafood fool`), -1)) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindSubmatch() { |
| | re := regexp.MustCompile(`foo(.?)`) |
| | fmt.Printf("%q\n", re.FindSubmatch([]byte(`seafood fool`))) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_Match() { |
| | re := regexp.MustCompile(`foo.?`) |
| | fmt.Println(re.Match([]byte(`seafood fool`))) |
| | fmt.Println(re.Match([]byte(`something else`))) |
| |
|
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindString() { |
| | re := regexp.MustCompile(`foo.?`) |
| | fmt.Printf("%q\n", re.FindString("seafood fool")) |
| | fmt.Printf("%q\n", re.FindString("meat")) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindStringIndex() { |
| | re := regexp.MustCompile(`ab?`) |
| | fmt.Println(re.FindStringIndex("tablett")) |
| | fmt.Println(re.FindStringIndex("foo") == nil) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindStringSubmatch() { |
| | re := regexp.MustCompile(`a(x*)b(y|z)c`) |
| | fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-")) |
| | fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-")) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAllString() { |
| | re := regexp.MustCompile(`a.`) |
| | fmt.Println(re.FindAllString("paranormal", -1)) |
| | fmt.Println(re.FindAllString("paranormal", 2)) |
| | fmt.Println(re.FindAllString("graal", -1)) |
| | fmt.Println(re.FindAllString("none", -1)) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAllStringSubmatch() { |
| | re := regexp.MustCompile(`a(x*)b`) |
| | fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-", -1)) |
| | fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-", -1)) |
| | fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-axb-", -1)) |
| | fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-ab-", -1)) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAllStringSubmatchIndex() { |
| | re := regexp.MustCompile(`a(x*)b`) |
| | |
| | |
| | |
| | fmt.Println(re.FindAllStringSubmatchIndex("-ab-", -1)) |
| | fmt.Println(re.FindAllStringSubmatchIndex("-axxb-", -1)) |
| | fmt.Println(re.FindAllStringSubmatchIndex("-ab-axb-", -1)) |
| | fmt.Println(re.FindAllStringSubmatchIndex("-axxb-ab-", -1)) |
| | fmt.Println(re.FindAllStringSubmatchIndex("-foo-", -1)) |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindSubmatchIndex() { |
| | re := regexp.MustCompile(`a(x*)b`) |
| | |
| | |
| | |
| | fmt.Println(re.FindSubmatchIndex([]byte("-ab-"))) |
| | fmt.Println(re.FindSubmatchIndex([]byte("-axxb-"))) |
| | fmt.Println(re.FindSubmatchIndex([]byte("-ab-axb-"))) |
| | fmt.Println(re.FindSubmatchIndex([]byte("-axxb-ab-"))) |
| | fmt.Println(re.FindSubmatchIndex([]byte("-foo-"))) |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_Longest() { |
| | re := regexp.MustCompile(`a(|b)`) |
| | fmt.Println(re.FindString("ab")) |
| | re.Longest() |
| | fmt.Println(re.FindString("ab")) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_MatchString() { |
| | re := regexp.MustCompile(`(gopher){2}`) |
| | fmt.Println(re.MatchString("gopher")) |
| | fmt.Println(re.MatchString("gophergopher")) |
| | fmt.Println(re.MatchString("gophergophergopher")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_NumSubexp() { |
| | re0 := regexp.MustCompile(`a.`) |
| | fmt.Printf("%d\n", re0.NumSubexp()) |
| |
|
| | re := regexp.MustCompile(`(.*)((a)b)(.*)a`) |
| | fmt.Println(re.NumSubexp()) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_ReplaceAll() { |
| | re := regexp.MustCompile(`a(x*)b`) |
| | fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("T"))) |
| | fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1"))) |
| | fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W"))) |
| | fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W"))) |
| |
|
| | re2 := regexp.MustCompile(`a(?P<1W>x*)b`) |
| | fmt.Printf("%s\n", re2.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W"))) |
| | fmt.Printf("%s\n", re2.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W"))) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_ReplaceAllLiteralString() { |
| | re := regexp.MustCompile(`a(x*)b`) |
| | fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T")) |
| | fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "$1")) |
| | fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "${1}")) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_ReplaceAllString() { |
| | re := regexp.MustCompile(`a(x*)b`) |
| | fmt.Println(re.ReplaceAllString("-ab-axxb-", "T")) |
| | fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1")) |
| | fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W")) |
| | fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W")) |
| |
|
| | re2 := regexp.MustCompile(`a(?P<1W>x*)b`) |
| | fmt.Printf("%s\n", re2.ReplaceAllString("-ab-axxb-", "$1W")) |
| | fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W")) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_ReplaceAllStringFunc() { |
| | re := regexp.MustCompile(`[^aeiou]`) |
| | fmt.Println(re.ReplaceAllStringFunc("seafood fool", strings.ToUpper)) |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_SubexpNames() { |
| | re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`) |
| | fmt.Println(re.MatchString("Alan Turing")) |
| | fmt.Printf("%q\n", re.SubexpNames()) |
| | reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1]) |
| | fmt.Println(reversed) |
| | fmt.Println(re.ReplaceAllString("Alan Turing", reversed)) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_SubexpIndex() { |
| | re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`) |
| | fmt.Println(re.MatchString("Alan Turing")) |
| | matches := re.FindStringSubmatch("Alan Turing") |
| | lastIndex := re.SubexpIndex("last") |
| | fmt.Printf("last => %d\n", lastIndex) |
| | fmt.Println(matches[lastIndex]) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_Split() { |
| | a := regexp.MustCompile(`a`) |
| | fmt.Println(a.Split("banana", -1)) |
| | fmt.Println(a.Split("banana", 0)) |
| | fmt.Println(a.Split("banana", 1)) |
| | fmt.Println(a.Split("banana", 2)) |
| | zp := regexp.MustCompile(`z+`) |
| | fmt.Println(zp.Split("pizza", -1)) |
| | fmt.Println(zp.Split("pizza", 0)) |
| | fmt.Println(zp.Split("pizza", 1)) |
| | fmt.Println(zp.Split("pizza", 2)) |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_Expand() { |
| | content := []byte(` |
| | # comment line |
| | option1: value1 |
| | option2: value2 |
| | |
| | # another comment line |
| | option3: value3 |
| | `) |
| |
|
| | |
| | pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) |
| |
|
| | |
| | |
| | template := []byte("$key=$value\n") |
| |
|
| | result := []byte{} |
| |
|
| | |
| | for _, submatches := range pattern.FindAllSubmatchIndex(content, -1) { |
| | |
| | |
| | result = pattern.Expand(result, template, content, submatches) |
| | } |
| | fmt.Println(string(result)) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_ExpandString() { |
| | content := ` |
| | # comment line |
| | option1: value1 |
| | option2: value2 |
| | |
| | # another comment line |
| | option3: value3 |
| | ` |
| |
|
| | |
| | pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) |
| |
|
| | |
| | |
| | template := "$key=$value\n" |
| |
|
| | result := []byte{} |
| |
|
| | |
| | for _, submatches := range pattern.FindAllStringSubmatchIndex(content, -1) { |
| | |
| | |
| | result = pattern.ExpandString(result, template, content, submatches) |
| | } |
| | fmt.Println(string(result)) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindIndex() { |
| | content := []byte(` |
| | # comment line |
| | option1: value1 |
| | option2: value2 |
| | `) |
| | |
| | pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) |
| |
|
| | loc := pattern.FindIndex(content) |
| | fmt.Println(loc) |
| | fmt.Println(string(content[loc[0]:loc[1]])) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAllSubmatchIndex() { |
| | content := []byte(` |
| | # comment line |
| | option1: value1 |
| | option2: value2 |
| | `) |
| | |
| | pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) |
| | allIndexes := pattern.FindAllSubmatchIndex(content, -1) |
| | for _, loc := range allIndexes { |
| | fmt.Println(loc) |
| | fmt.Println(string(content[loc[0]:loc[1]])) |
| | fmt.Println(string(content[loc[2]:loc[3]])) |
| | fmt.Println(string(content[loc[4]:loc[5]])) |
| | } |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRegexp_FindAllIndex() { |
| | content := []byte("London") |
| | re := regexp.MustCompile(`o.`) |
| | fmt.Println(re.FindAllIndex(content, 1)) |
| | fmt.Println(re.FindAllIndex(content, -1)) |
| | |
| | |
| | |
| | } |
| |
|