Debian Codegraph @

re2.go

raw symbols SCIP
1 package re2
2
3 import (
4 "regexp"
5
6 "github.com/betterleaks/go-re2/internal"
7 )
8
9 type Regexp = internal.Regexp
10
11 // MatchString reports whether the string s
12 // contains any match of the regular expression pattern.
13 // More complicated queries need to use Compile and the full Regexp interface.
14 func MatchString(pattern string, s string) (matched bool, err error) {
15 re, err := Compile(pattern)
16 if err != nil {
17 return false, err
18 }
19 defer internal.Release(re)
20 return re.MatchString(s), nil
21 }
22
23 // Match reports whether the byte slice b
24 // contains any match of the regular expression pattern.
25 // More complicated queries need to use Compile and the full Regexp interface.
26 func Match(pattern string, b []byte) (matched bool, err error) {
27 re, err := Compile(pattern)
28 if err != nil {
29 return false, err
30 }
31 defer internal.Release(re)
32 return re.Match(b), nil
33 }
34
35 // Compile parses a regular expression and returns, if successful,
36 // a Regexp object that can be used to match against text.
37 //
38 // When matching against text, the regexp returns a match that
39 // begins as early as possible in the input (leftmost), and among those
40 // it chooses the one that a backtracking search would have found first.
41 // This so-called leftmost-first matching is the same semantics
42 // that Perl, Python, and other implementations use, although this
43 // package implements it without the expense of backtracking.
44 // For POSIX leftmost-longest matching, see CompilePOSIX.
45 func Compile(expr string) (*Regexp, error) {
46 return internal.Compile(expr, internal.CompileOptions{}) //nolint:wrapcheck // just a method forwarder
47 }
48
49 // CompilePOSIX is like Compile but restricts the regular expression
50 // to POSIX ERE (egrep) syntax and changes the match semantics to
51 // leftmost-longest.
52 //
53 // That is, when matching against text, the regexp returns a match that
54 // begins as early as possible in the input (leftmost), and among those
55 // it chooses a match that is as long as possible.
56 // This so-called leftmost-longest matching is the same semantics
57 // that early regular expression implementations used and that POSIX
58 // specifies.
59 //
60 // However, there can be multiple leftmost-longest matches, with different
61 // submatch choices, and here this package diverges from POSIX.
62 // Among the possible leftmost-longest matches, this package chooses
63 // the one that a backtracking search would have found first, while POSIX
64 // specifies that the match be chosen to maximize the length of the first
65 // subexpression, then the second, and so on from left to right.
66 // The POSIX rule is computationally prohibitive and not even well-defined.
67 // See https://swtch.com/~rsc/regexp/regexp2.html#posix for details.
68 func CompilePOSIX(expr string) (*Regexp, error) {
69 return internal.Compile(expr, internal.CompileOptions{Longest: true, Posix: true}) //nolint:wrapcheck // just a method forwarder
70 }
71
72 // MustCompile is like Compile but panics if the expression cannot be parsed.
73 // It simplifies safe initialization of global variables holding compiled regular
74 // expressions.
75 func MustCompile(str string) *Regexp {
76 re, err := Compile(str)
77 if err != nil {
78 panic(`regexp: Compile(` + internal.QuoteForError(str) + `): ` + err.Error())
79 }
80 return re
81 }
82
83 // MustCompilePOSIX is like CompilePOSIX but panics if the expression cannot be parsed.
84 // It simplifies safe initialization of global variables holding compiled regular
85 // expressions.
86 func MustCompilePOSIX(str string) *Regexp {
87 regexp, err := CompilePOSIX(str)
88 if err != nil {
89 panic(`regexp: CompilePOSIX(` + internal.QuoteForError(str) + `): ` + err.Error())
90 }
91 return regexp
92 }
93
94 // QuoteMeta returns a string that escapes all regular expression metacharacters
95 // inside the argument text; the returned string is a regular expression matching
96 // the literal text.
97 func QuoteMeta(s string) string {
98 return regexp.QuoteMeta(s)
99 }