Returns the first part of a string that matches a regular expression, or an empty string.
str.match searches the source for the first match of a regular expression and returns that text. Patterns such as "[0-9]+" pick out the first run of digits.
When nothing matches, the result is an empty string. When the pattern itself is invalid, the script stops with an error.
Syntax
str.match(source: simple string, regex: simple string) -> simple string
str.match(source: series string, regex: series string) -> series stringArguments
| Name | Type | Default | Description |
|---|---|---|---|
| source | simple/ | required | The text the regular expression is run against. |
| regex | simple/ | required | The regular expression to look for. |
Returns
simple/series string: the matching text, or an empty string.
Remarks
- Escape special characters with a double backslash in a Pine string, as in "\\d+".
- On Sidechair: patterns use JavaScript regular expression syntax.
Example
//@version=6
indicator("Match")
digits = str.match("level 4500 held", "[0-9]+")
plot(str.tonumber(digits), "Level", color = color.purple, style = plot.style_stepline)See also
Runs on Sidechair