-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexp.go
37 lines (33 loc) · 834 Bytes
/
regexp.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
package stringutil
import (
"regexp"
"strings"
)
func regexpSubstrings(expr, s, exp_head, exp_end string, n int) (r []string) {
strs := regexp.MustCompile(expr).FindAllString(s, -1)
for i, str := range strs {
if n > 0 && i >= n {
break
}
var start, end int = 0, 0
if len(exp_head) != 0 {
start = strings.Index(str, exp_head) + len(exp_head)
}
if len(exp_end) != 0 {
end = strings.LastIndex(str, exp_end)
}
if str = Substring(str, start, end); len(str) != 0 {
r = append(r, str)
}
}
return
}
func RegexpSubstrings(expr, s, exp_head, exp_end string) []string {
return regexpSubstrings(expr, s, exp_head, exp_end, -1)
}
func RegexpSubstring(expr, s, exp_head, exp_end string) string {
if strs := regexpSubstrings(expr, s, exp_head, exp_end, 1); len(strs) != 0 {
return strs[0]
}
return ""
}