-
Notifications
You must be signed in to change notification settings - Fork 0
/
47-regular-expressions.go
44 lines (28 loc) · 982 Bytes
/
47-regular-expressions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//https://gobyexample.com/regular-expressions
package main
import (
"bytes"
"fmt"
"regexp"
)
func main() {
match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
fmt.Println(match)
r, _ := regexp.Compile("p([a-z]+)ch")
fmt.Println("01-", r.MatchString("peach"))
fmt.Println("02-", r.FindString("peach punch"))
fmt.Println("03-", r.FindStringIndex("peach punch"))
fmt.Println("04-", r.FindStringSubmatch("peach punch"))
fmt.Println("05-", r.FindStringSubmatchIndex("peach punch"))
fmt.Println("06-", r.FindAllString("peach punch pinch", -1))
fmt.Println("07-", r.FindAllStringSubmatchIndex(
"peach punch pinch", -1))
fmt.Println("08-", r.FindAllString("peach punch pinch", 2))
fmt.Println("09-", r.Match([]byte("peach")))
r = regexp.MustCompile("p([a-z]+)ch")
fmt.Println("10-", r)
fmt.Println("11-", r.ReplaceAllString("a peach", "<fruit>"))
in := []byte("a peach")
out := r.ReplaceAllFunc(in, bytes.ToUpper)
fmt.Println("12-", string(out))
}