strings.FindRESubmatch
- Syntax
- strings.FindRESubmatch PATTERN STRING [LIMIT]
- Returns
- [][]string
- Alias
- findRESubmatch
Usage
The strings.FindRESubmatch function returns a slice of all successive matches of the regular expression. Each element is a slice of strings holding the text of the leftmost match of the regular expression and the matches, if any, of its subexpressions.
If a subexpression matches an empty string, or does not participate in the match, its element is an empty string.
By default, the function finds all matches. To limit the number of matches, pass the optional LIMIT argument. A return value of nil indicates no match.
When specifying the regular expression, use a raw string literal (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes.
Go’s regular expression package implements the RE2 syntax. The RE2 syntax is a subset of that accepted by PCRE, roughly speaking, and with various caveats. Note that the RE2 \C escape sequence is not supported.
You can write and test your regular expression using regex101.com. Be sure to select the Go flavor before you begin.
Examples
{{ strings.FindRESubmatch `a(x+)b` "-axb-axxb-" }} → [[axb x] [axxb xx]]
{{ strings.FindRESubmatch `a(x+)b` "-axb-axxb-" 1 }} → [[axb x]]
{{ strings.FindRESubmatch `a(x+)b` "-ab-" }} → []Markdown containing two links:
- [Example](https://example.org)
- [Hugo](https://gohugo.io)Capturing the text and destination of each link in the rendered content:
{{ $regex := `<a\s+href="(.+?)">(.+?)</a>` }}
{{ range strings.FindRESubmatch $regex .Content }}
{{ index . 2 }}: {{ index . 1 }}
{{ end }}The rendered output:
Example: https://example.org
Hugo: https://gohugo.io