-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
61 lines (51 loc) · 2.05 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"regexp"
)
func main() {
// --- Simple Matching ---
pattern1 := `^x.`
sample1 := "xxyyyz123"
ok, err := regexp.MatchString(pattern1, sample1)
fmt.Println("Ok (should be true):", ok)
fmt.Println("Err (should be nil):", err)
// The function `MatchString` returns an error when the regexp pattern is invalid.
invalidPattern := `[[:invalid:]]`
_, err = regexp.MatchString(invalidPattern, sample1)
fmt.Println("Err (should be non-nil):", err)
// --- Regexp compilation ---
// For more complex regexp pattern, compile it using `Compile` or `MustCompile`.
// The difference between the functions is that the latter panics if the pattern is invalid.
_, err = regexp.Compile(invalidPattern)
fmt.Println("Err (should be non-nil):", err)
// To recover from a panic properly we need to 1. Use defer to call recover(),
// 2. The function that panics must be in a function scope so that once we recover
// we return back to the parent and the flow continues.
func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from a panic", r)
}
}()
_ = regexp.MustCompile(invalidPattern) // Panics
}()
// --- Find ---
pattern2 := `<([a-z]+)>`
sample2 := "<title><123><TITLE><footer>"
re2, err := regexp.Compile(pattern2)
fmt.Println("FindString:", re2.FindString(sample2))
fmt.Println("FindStringIndex:", re2.FindStringIndex(sample2))
fmt.Println("FindStringSubmatch", re2.FindStringSubmatch(sample2))
fmt.Println("FindStringSubmatchIndex", re2.FindStringSubmatchIndex(sample2))
fmt.Println("FindAllString", re2.FindAllString(sample2, -1))
fmt.Println("FindAllStringIndex", re2.FindAllStringIndex(sample2, -1))
fmt.Println("FindAllStringSubmatch", re2.FindAllStringSubmatch(sample2, -1))
fmt.Println("FindAllStringSubmatchIndex", re2.FindAllStringSubmatchIndex(sample2, -1))
// --- Replace ---
pattern3 := `//.*@`
sample3 := "mongodb://nobody:secrets@localhost:27017/go-recipes"
re3, _ := regexp.Compile(pattern3)
replaced := re3.ReplaceAllString(sample3, "//*****:*****@")
fmt.Println("New string", replaced)
}