Skip to content

Commit f49f484

Browse files
committed
Added more info to readme and example of FindNextMatch usage
1 parent 955551b commit f49f484

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ Group 0 is embedded in the Match. Group 0 is an automatically-assigned group th
3939

4040
The __last__ capture is embedded in each group, so `g.String()` will return the same thing as `g.Capture.String()` and `g.Captures[len(g.Captures)-1].String()`.
4141

42+
If you want to find multiple matches from a single input string you should use the `FindNextMatch` method. For example, to implement a function similar to `regexp.FindAllString`:
43+
44+
```go
45+
func regexp2FindAllString(re *regexp2.Regexp, s string) []string {
46+
var matches []string
47+
m, _ := re.FindStringMatch(s)
48+
for m != nil {
49+
matches = append(matches, m.String())
50+
m, _ = re.FindNextMatch(m)
51+
}
52+
return matches
53+
}
54+
```
55+
56+
`FindNextMatch` is optmized so that it re-uses the underlying string/rune slice.
57+
58+
The internals of `regexp2` always operate on `[]rune` so `Index` and `Length` data in a `Match` always reference a position in `rune`s rather than `byte`s (even if the input was given as a string). This is a dramatic difference between `regexp` and `regexp2`. It's advisable to use the provided `String()` methods to avoid having to work with indices.
59+
4260
## Compare `regexp` and `regexp2`
4361
| Category | regexp | regexp2 |
4462
| --- | --- | --- |

0 commit comments

Comments
 (0)