HUGO
Menu
GitHub 87548 stars Mastodon

strings.FindRE

Returns a slice of strings that match the regular expression.

Syntax

strings.FindRE PATTERN INPUT [LIMIT]

Returns

[]string

Alias

findRE

By default, findRE finds all matches. You can limit the number of matches with an optional LIMIT argument.

在指定正则表达式时,使用原始 [字符串字面量](反引号)而不是解释字符串字面量(双引号)以简化语法。使用解释字符串字面量时,您必须转义反斜杠。

Go 的正则表达式包实现了 [RE2 语法]。RE2 语法是 PCRE 接受的内容的子集(粗略地说),并带有各种 [注意事项]。请注意,不支持 RE2 \C 转义序列。

This example returns a slice of all second level headings (h2 elements) within the rendered .Content:

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

The s flag causes . to match \n as well, allowing us to find an h2 element that contains newlines.

To limit the number of matches to one:

{{ findRE `(?s)<h2.*?>.*?</h2>` .Content 1 }}

You can write and test your regular expression using regex101.com. Be sure to select the Go flavor before you begin.


Last updated: January 1, 0001
Improve this page