Sidechair

str.match()

Browse the reference

Returns the first part of a string that matches a regular expression, or an empty string.

Try it in the editor

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

Pine Script
str.match(source: simple string, regex: simple string) -> simple string
str.match(source: series string, regex: series string) -> series string

Arguments

NameTypeDefaultDescription
sourcesimple/series stringrequiredThe text the regular expression is run against.
regexsimple/series stringrequiredThe 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

Pine ScriptPull the number out of textOpen in editor
//@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