-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathextended.go
40 lines (33 loc) · 1.11 KB
/
extended.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
// Package regext enables extended regex, where spaces are ignored and inline comments are supported.
package regext
import (
"regexp"
"strings"
)
var (
whitespaceRe = regexp.MustCompile(`\s+`)
commentsRe = regexp.MustCompile(`(?:\\#|[^#])*`)
)
// MustCompile compiles expr as a regular expression in a Perl-like "extended" mode where whitespace characters and comments are ignored
func MustCompile(expr string) *regexp.Regexp {
return regexp.MustCompile(extendedRegexp(expr))
}
// Compile compiles expr as a regular expression in a Perl-like "extended" mode where whitespace characters and comments are ignored
func Compile(expr string) (*regexp.Regexp, error) {
return regexp.Compile(extendedRegexp(expr))
}
func extendedRegexp(expr string) string {
expr = removeComments(expr)
expr = removeWhitespace(expr)
return expr
}
func removeComments(expr string) string {
lines := strings.Split(expr, "\n")
for i := range lines {
lines[i] = commentsRe.FindStringSubmatch(lines[i])[0]
}
return strings.Join(lines, "\n")
}
func removeWhitespace(expr string) string {
return strings.Join(whitespaceRe.Split(expr, -1), "")
}