Regular expression functions
Overview
These functions perform matching using the standard Perl-style regular expression syntax. See the Regular expression reference for links to detailed information on constructing regular expressions.
RegexCount
Returns the number of times that text matches the given regex.
Syntax
RegexCount( text, regex )
The RegexCount function syntax has the following arguments.
Part | Description |
---|---|
text | (Required) Any expression that produces a text result. |
regex | (Required) A literal string containing a regular expression. |
Remarks
The function RegexCount counts the matches of regex in text.
Examples
RegexCount("abc123def456", /\d+/)
returns 2.RegexCount("abc123def456", /\d/)
returns 6.
RegexExtractAll
Finds all substrings where text that matches regex, and returns or replaces them.
Syntax
RegexExtractAll( text, regex, separator )
RegexExtractAll( text, regex, separator, replacement )
The RegexExtractAll function syntax has the following arguments.
Part | Description |
---|---|
text | (Required) Any expression that produces a text result. |
regex | (Required) A literal string containing a regular expression. |
separator | (Required) A literal string containing a separator character. |
replacement | (Optional) Any expression that produces a text value. |
Remarks
The first form of the function RegexExtractAll
finds all substrings where text matches regex, and returns them separated by separator (which may be the empty string "").
The second form of the function RegexExtractAll
finds all substrings where text matches regex, replaces them with the replacement string, and returns them separated by separator. References (such as \1) and Perl-style substitution operators are supported in replacement.
Examples
RegexExtractAll("abc123def456", /\d/, ",")
returns 1,2,3,4,5,6.RegexExtractAll("abc123def456", /\d+/, ",")
returns 123,456.RegexExtractAll("abc123", /(\d)/, ",", "n=\1")
returns n=1,n=2,n=3.RegexExtractAll("abc123def456", /(\d+)/, "|", "n=\1")
returns n=123|n=456.
RegexExtractFirst
Finds the first substring where text that matches regex, and returns or replaces it.
Syntax
RegexExtractFirst( text, regex )
RegexExtractFirst( text, regex, replacement )
The RegexExtractFirst function syntax has the following arguments.
Part | Description |
---|---|
text | (Required) Any expression that produces a text result. |
regex | (Required) A literal string containing a regular expression. |
replacement | (Optional) Any expression that produces a text value. |
Remarks
The function RegexExtractFirst
finds the first substring where text matches regex, and returns it or (if replacement is specified) replaces it with the replacement string. References (such as \1) and Perl-style substitution operators are supported in replacement.
Examples
RegexExtractFirst("abc123", /\d/)
returns 1.RegexExtractFirst("abc123", /\d+/)
returns 123.RegexExtractFirst("abc123", /(\d+)/, "result=\1")
returns result=123.
RegexMatch
Returns a Boolean value, which is True if text matches regex, and False otherwise.
Syntax
RegexMatch( text, regex )
The RegexMatch function syntax has the following arguments.
Part | Description |
---|---|
text | (Required) Any expression that produces a text result. |
regex | (Required) A literal string containing a regular expression. |
Remarks
The function RegexMatch
tests whether a string matches a given regular expression or not. It returns True
if text matches regex. The regex is unanchored unless the ^ and/or $ symbols are used explicitly.
Examples
RegexMatch("abc123", /\d+/)
returnsTrue
.RegexMatch("abc123", /^\d+/)
returnsFalse
.
RegexReplaceAll
Replaces all occurrences in text of text matching regex with replacement.
Syntax
RegexReplaceAll( text, pattern, replacement )
The RegexReplaceAll function syntax has the following arguments.
Part | Description |
---|---|
text | (Required) Any expression that produces a text result. |
regex | (Required) A literal string containing a regular expression. |
replacement | (Required) Any expression that produces a text value. |
Remarks
The function RegularExprReplaceAll
finds all substrings where text matches regex, and replaces them with replacement. References (such as \1) and Perl-style substitution operators are supported in replacement. Replaced text is not re-matched.
Examples
RegexReplaceAll("abc123", /\d/, "X")
returnsabcXXX
.RegexReplaceAll("abc123def", /(\d+)/, /<\1>/)
returnsabc<123>def
.
RegexReplaceFirst
Replaces the first occurrence in text of text matching the given regex with replacement.
Syntax
RegexReplaceFirst( text, regex, replacement )
The RegexReplaceFirst function syntax has the following arguments.
Part | Description |
---|---|
text | (Required) Any expression that produces a text result. |
regex | (Required) A literal string containing a regular expression. |
replacement | (Required) Any expression that produces a text value. |
Remarks
The function RegularExprReplaceFirst
finds the first substring where text matches regex, and replaces it with replacement. References (such as \1) and Perl-style substitution operators are supported in replacement.
Examples
RegexReplaceFirst("abc123", /\d/, "X")
returnsabcX23
.RegexReplaceFirst("abc123", /\d+/, "X")
returnsabcX
.