/
utils.go
51 lines (38 loc) · 886 Bytes
/
utils.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
package datatype
import "regexp"
type RegexpPair struct {
Value string
Match bool
}
// 正则表达式分割字符串
func RegexpSplit(expr string, str string) []RegexpPair {
var data []RegexpPair
if m, err := regexp.Compile(expr); err == nil {
for {
loc := m.FindStringIndex(str)
if loc == nil {
if str != "" {
data = append(data, RegexpPair{Value: str})
}
break
}
if loc[0] > 0 {
data = append(data, RegexpPair{Value: str[:loc[0]]})
}
match := str[loc[0]:loc[1]]
data = append(data, RegexpPair{Value: match, Match: true})
str = str[loc[1]:]
}
} else {
data = append(data, RegexpPair{Value: str})
}
return data
}
// 字符串反转
func ReverseString(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}