-
Notifications
You must be signed in to change notification settings - Fork 21
/
regex.go
134 lines (111 loc) · 3.62 KB
/
regex.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"fmt"
"log"
"regexp"
"strings"
)
func main() {
multilineRegexp()
//regexp1()
//regexp2()
}
func multilineRegexp() {
fmt.Println("--- multine regexp ---")
str := `
CREATE OR REPLACE FUNCTION himom()
RETURNS number
LANGUAGE SQL
AS $himom$
SELECT 'himom';
$hidad$;
`
//r := regexp.MustCompile(`(?is)CREATE.*\s+AS\s+.* (\$[[:alnum:]]*\$)`)
r := regexp.MustCompile(`(?is)CREATE.*\s+AS\s+((?U).*)(\$[^\$]*\$)`)
match := r.FindStringSubmatch(str)
fmt.Printf("%q\n", match)
}
func regexp1() {
fmt.Println("--- regexp1 ---")
includeRegex, err := regexp.Compile(`^\s*include\("(\\\"|[^"])+"\);`)
if err != nil {
log.Fatal(err)
}
for _, line := range strings.Split(`
foo
include "abc.def"
include("file.js");
include "me\"to\""
include("please\"!\"");
nothing here
`, "\n") {
if includeRegex.Match([]byte(line)) {
includeFile := includeRegex.FindString(line)
fmt.Println("INCLUDE", includeFile)
} else {
fmt.Printf("no match for \"%s\"\n", line)
}
}
}
func regexp2() {
fmt.Println("--- regexp2 ---")
// This tests whether a pattern matches a string.
match1, _ := regexp.MatchString(`(?i)(^|\s)FROM\s`, " from t_user")
fmt.Println(match1)
// This tests whether a pattern matches a string.
match, _ := regexp.MatchString(`p([a-z]+)ch`, "peach")
fmt.Println(match)
// Above we used a string pattern directly, but for
// other regexp tasks you'll need to `Compile` an
// optimized `Regexp` struct.
r, _ := regexp.Compile("p([a-z]+)ch")
// Many methods are available on these structs. Here's
// a match test like we saw earlier.
fmt.Println(r.MatchString("peach"))
// This finds the match for the regexp.
fmt.Println(r.FindString("peach punch"))
// The also finds the first match but returns the
// start and end indexes for the match instead of the
// matching text.
fmt.Println(r.FindStringIndex("peach punch"))
// The `Submatch` variants include information about
// both the whole-pattern matches and the submatches
// within those matches. For example this will return
// information for both `p([a-z]+)ch` and `([a-z]+)`.
fmt.Println(r.FindStringSubmatch("peach punch"))
// Similarly this will return information about the
// indexes of matches and submatches.
fmt.Println(r.FindStringSubmatchIndex("peach punch"))
// The `All` variants of these functions apply to all
// matches in the input, not just the first. For
// example to find all matches for a regexp.
fmt.Println(r.FindAllString("peach punch pinch", -1))
// These `All` variants are available for the other
// functions we saw above as well.
fmt.Println(r.FindAllStringSubmatchIndex(
"peach punch pinch", -1))
// Providing a non-negative integer as the second
// argument to these functions will limit the number
// of matches.
fmt.Println(r.FindAllString("peach punch pinch", 2))
// Our examples above had string arguments and used
// names like `MatchString`. We can also provide
// `[]byte` arguments and drop `String` from the
// function name.
fmt.Println(r.Match([]byte("peach")))
// When creating constants with regular expressions
// you can use the `MustCompile` variation of
// `Compile`. A plain `Compile` won't work for
// constants because it has 2 return values.
r = regexp.MustCompile("p([a-z]+)ch")
fmt.Println(r)
// The `regexp` package can also be used to replace
// subsets of strings with other values.
fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))
// The `Func` variant allows you to transform matched
// text with a given function.
in := []byte("a peach")
out := r.ReplaceAllFunc(in, bytes.ToUpper)
fmt.Println(string(out))
}