@@ -22,13 +22,6 @@ import (
22
22
23
23
var errNoMatch = errors .New ("no match" )
24
24
25
- // Valid reports whether the given regular expression
26
- // is valid.
27
- func Valid (pattern string ) (bool , error ) {
28
- _ , err := regexp .Compile (pattern )
29
- return err == nil , err
30
- }
31
-
32
25
// Find returns a string holding the text of the leftmost match in s of
33
26
// the regular expression. It returns bottom if there was no match.
34
27
func Find (pattern , s string ) (string , error ) {
@@ -61,25 +54,37 @@ func FindAll(pattern, s string, n int) ([]string, error) {
61
54
return m , nil
62
55
}
63
56
64
- // FindSubmatch returns a list of strings holding the text of the leftmost match
65
- // of the regular expression in s and the matches, if any, of its
66
- // subexpressions. Submatches are matches of parenthesized subexpressions (also
67
- // known as capturing groups) within the regular expression, numbered from left
68
- // to right in order of opening parenthesis. Submatch 0 is the match of the
69
- // entire expression, submatch 1 the match of the first parenthesized
70
- // subexpression, and so on. It returns bottom for no match.
71
- func FindSubmatch (pattern , s string ) ([]string , error ) {
57
+ // FindAllNamedSubmatch is like FindAllSubmatch, but returns a map with the
58
+ // named used in capturing groups. See FindNamedSubmatch for an example on
59
+ // how to use named groups.
60
+ func FindAllNamedSubmatch (pattern , s string , n int ) ([]map [string ]string , error ) {
72
61
re , err := regexp .Compile (pattern )
73
62
if err != nil {
74
63
return nil , err
75
64
}
76
- m := re .FindStringSubmatch (s )
65
+ names := re .SubexpNames ()
66
+ if len (names ) == 0 {
67
+ return nil , errNoNamedGroup
68
+ }
69
+ m := re .FindAllStringSubmatch (s , n )
77
70
if m == nil {
78
71
return nil , errNoMatch
79
72
}
80
- return m , nil
73
+ result := make ([]map [string ]string , len (m ))
74
+ for i , m := range m {
75
+ r := make (map [string ]string , len (names )- 1 )
76
+ for k , name := range names {
77
+ if name != "" {
78
+ r [name ] = m [k ]
79
+ }
80
+ }
81
+ result [i ] = r
82
+ }
83
+ return result , nil
81
84
}
82
85
86
+ var errNoNamedGroup = errors .New ("no named groups" )
87
+
83
88
// FindAllSubmatch finds successive matches as returned by FindSubmatch,
84
89
// observing the rules of FindAll. It returns bottom for no match.
85
90
func FindAllSubmatch (pattern , s string , n int ) ([][]string , error ) {
@@ -94,8 +99,6 @@ func FindAllSubmatch(pattern, s string, n int) ([][]string, error) {
94
99
return m , nil
95
100
}
96
101
97
- var errNoNamedGroup = errors .New ("no named groups" )
98
-
99
102
// FindNamedSubmatch is like FindSubmatch, but returns a map with the names used
100
103
// in capturing groups.
101
104
//
@@ -126,31 +129,28 @@ func FindNamedSubmatch(pattern, s string) (map[string]string, error) {
126
129
return r , nil
127
130
}
128
131
129
- // FindAllNamedSubmatch is like FindAllSubmatch, but returns a map with the
130
- // named used in capturing groups. See FindNamedSubmatch for an example on
131
- // how to use named groups.
132
- func FindAllNamedSubmatch (pattern , s string , n int ) ([]map [string ]string , error ) {
132
+ // FindSubmatch returns a list of strings holding the text of the leftmost match
133
+ // of the regular expression in s and the matches, if any, of its
134
+ // subexpressions. Submatches are matches of parenthesized subexpressions (also
135
+ // known as capturing groups) within the regular expression, numbered from left
136
+ // to right in order of opening parenthesis. Submatch 0 is the match of the
137
+ // entire expression, submatch 1 the match of the first parenthesized
138
+ // subexpression, and so on. It returns bottom for no match.
139
+ func FindSubmatch (pattern , s string ) ([]string , error ) {
133
140
re , err := regexp .Compile (pattern )
134
141
if err != nil {
135
142
return nil , err
136
143
}
137
- names := re .SubexpNames ()
138
- if len (names ) == 0 {
139
- return nil , errNoNamedGroup
140
- }
141
- m := re .FindAllStringSubmatch (s , n )
144
+ m := re .FindStringSubmatch (s )
142
145
if m == nil {
143
146
return nil , errNoMatch
144
147
}
145
- result := make ([]map [string ]string , len (m ))
146
- for i , m := range m {
147
- r := make (map [string ]string , len (names )- 1 )
148
- for k , name := range names {
149
- if name != "" {
150
- r [name ] = m [k ]
151
- }
152
- }
153
- result [i ] = r
154
- }
155
- return result , nil
148
+ return m , nil
149
+ }
150
+
151
+ // Valid reports whether the given regular expression
152
+ // is valid.
153
+ func Valid (pattern string ) (bool , error ) {
154
+ _ , err := regexp .Compile (pattern )
155
+ return err == nil , err
156
156
}
0 commit comments