match
Returns the first match of a regular expression, or one of its capture groups.
Details #
match(text, pattern, group?) takes two or three arguments.
Returns the first match of pattern as text. With no group, that is the whole match.
group is a number: 0 is the whole match, 1 the first parenthesised group, and so on.
Returns
null(nothing) when the pattern doesn't match, so test the result before using it:match(s, "[0-9]+") ? ... : ....
The pattern is a regular expression (ICU syntax) and is case-sensitive.
Parameters #
Takes 2 to 3 arguments.
{{ match(text, b, c?) }}
Examples #
match("order 1234 shipped", "[0-9]+")1234match("key=value", "(.*)=(.*)", 2)valuematch("no digits", "[0-9]+") == nulltrueGotchas #
A group number the pattern doesn't have is an error:
match() group 3 doesn't exist in that pattern. A group that exists but matched nothing givesnull.An invalid pattern is an error:
'(' isn't a valid regular expression.Matching is not anchored. Add
^and$to test the whole text.Backslashes in quotes need doubling (
"\\d+"), or use[0-9].