Regular Expressions
Regular Expressions
原文:https://gobyexample.com/regular-expressions
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
| package main
import (
"bytes"
"fmt"
"regexp"
)
func main() {
match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
fmt.Println(match) // true
r, _ := regexp.Compile("p([a-z]+)ch")
fmt.Println(r.MatchString("peach")) // true
fmt.Println(r.FindString("peach punch")) // peach
fmt.Println("idx:", r.FindStringIndex("peach punch")) // idx: [0 5]
fmt.Println(r.FindStringSubmatch("peach punch")) // [peach ea]
fmt.Println(r.FindStringSubmatchIndex("peach punch")) // [0 5 1 3]
fmt.Println(r.FindAllString("peach punch pinch", -1)) // [peach punch pinch]
fmt.Println("all:", r.FindAllStringSubmatchIndex(
"peach punch pinch", -1)) // all: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]
fmt.Println(r.FindAllString("peach punch pinch", 2)) // [peach punch]
fmt.Println(r.Match([]byte("peach"))) // true
r = regexp.MustCompile("p([a-z]+)ch")
fmt.Println("regexp:", r) // regexp: p([a-z]+)ch
fmt.Println(r.ReplaceAllString("a peach", "<fruit>")) // a <fruit>
in := []byte("a peach")
out := r.ReplaceAllFunc(in, bytes.ToUpper)
fmt.Println(string(out)) // a PEACH
}
|