HUGO
Menu
GitHub 90272 stars Mastodon

strings.FindRE

Returns a slice of strings that match the regular expression.
Syntax
strings.FindRE PATTERN STRING [LIMIT]
Returns
[]string
Alias
findRE

Usage

The strings.FindRE function returns a slice of strings that match the regular expression. By default, the function finds all matches. To limit the number of matches, pass the optional LIMIT argument.

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.FindRE `a.` "paranormal" }} → [ar an al]
{{ strings.FindRE `a.` "paranormal" 2 }} → [ar an]

Finding all second-level headings (h2 elements) within the rendered content, where the s flag causes . to match \n as well:

{{ strings.FindRE `(?s)<h2.*?>.*?</h2>` .Content }}