-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.go
52 lines (44 loc) · 1.47 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
package regex
import (
"regexp"
"strings"
)
// Find returns the first match of the pattern in the string
func Find(pattern, str string) string {
re := regexp.MustCompile(pattern)
return re.FindString(str)
}
// FindAll returns all matches of the pattern in the string
func FindAll(pattern, str string) []string {
re := regexp.MustCompile(pattern)
return re.FindAllString(str, -1)
}
// FindAllIndex returns the indexes of all matches of the pattern in the string
func FindAllIndex(pattern, str string) []int {
re := regexp.MustCompile(pattern)
var indexes []int
for _, match := range re.FindAllStringIndex(str, -1) {
indexes = append(indexes, match[0])
}
return indexes
}
// FindIndex returns the first and last index of the first match of the pattern in the string
func FindIndex(pattern, str string) int {
re := regexp.MustCompile(pattern)
return re.FindStringIndex(str)[0]
}
// Match returns true if the pattern matches the string
func Match(pattern, str string) bool {
re := regexp.MustCompile(pattern)
return re.MatchString(str)
}
// Replace replaces the nb first matches of the pattern in the string with the new string
func Replace(pattern, str, new string, nb int) string {
re := regexp.MustCompile(pattern)
return strings.Replace(str, re.FindString(str), new, nb)
}
// ReplaceAll replaces all matches of the pattern in the string with the new string
func ReplaceAll(pattern, str, new string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(str, new)
}