zre.go 679 Bytes
Newer Older
Vladimir Barsukov's avatar
zre  
Vladimir Barsukov committed
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
package zre

import (
	"regexp"
)

var cache map[string]*regexp.Regexp

func init() {
	cache = make(map[string]*regexp.Regexp)
}

func New(s string) *regexp.Regexp {
	if r, ok := cache[s]; ok {
		return r
	}

	cache[s] = regexp.MustCompile(s)

	return cache[s]
}

func Replace(expr string, s, rep string) string {
	return New(expr).ReplaceAllString(s, rep)
}

func Match(expr, s string) bool {
	return New(expr).MatchString(s)
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
31
32
func FindAll(expr, s string) [][]string {
	return New(expr).FindAllStringSubmatch(s, -1)
Vladimir Barsukov's avatar
zre  
Vladimir Barsukov committed
33
34
35
36
37
38
39
40
41
}

func Find(expr, s string) string {
	return New(expr).FindString(s)
}

func FindSubmatch(expr, s string) []string {
	return New(expr).FindStringSubmatch(s)
}