PINE LIBRARY

RegexLib

OVERVIEW

This library contains regular expression (regex) search functions which are helpful, in particular, in reading configuration inputs.

  • feedRgx(): Searches for the first occurrence of `regex` pattern in the `src` and returns `src` split into parts as a tuple: `[<before the match>, <match>, <after the macth>]`. If no match returns `[src, "", ""]`

  • countRgx(): Counts `regex` occurrences in the `src`.

  • matchRgx(): Finds given `occurence` of `regex` pattern in `src` string.


NB! countRgx() and matchRgx() do not support `^` (beginning of the string placeholder), lookbehind some other complex patterns, because they works by cutting off the part of the string up to the first found occurence (inclusive) and then continuing the search on the remainder of the string. E.g. in a four line source `(?<=\n).+\n` should match the second and the third lines but matchRgx only matches the second line since after matching it continues to search in the remainder AFTER the match only.

FULL LIST OF FUNCTIONS AND PARAMETERS

method feedRgx(src, regex)
  Searches for the first occurrence of `regex` pattern in the `src` and returns `src` split into parts as a tuple: `[<before the match>, <match>, <after the macth>]`. If no match returns `[src,"",""]`
  Namespace types: series string, simple string, input string, const string
  Parameters:
    src (string): (string) String to search for the regex pattern
    regex (string): (string) RegEx pattern
  Returns: A tuple `[begS, matchS, endS]` where `begS` is the part of the `src` string from the beginning up to the
first occurence of the `regex` pattern (or up to the end if not found), `matchS` - the first occurrence of the
regex pattern and `endS` the part of the strinf following the first occurrence of the `regex` pattern.

method countRgx(src, regex)
  Counts `regex` occurrences in the `src`. ___NB!___ _Does not support `^` (beginning of the string
placeholder), lookbehind some other complex patterns, because it works by cutting off the part of the string up to
the first found occurence (inclusive) and then continuing the search on the remainder of the string. E.g. in a four line source `(?<=\n).+\n` should match the second and the third lines but matchRgx only
matches the second line since after matching it continues to search in the remainder AFTER the match only. _
  Namespace types: series string, simple string, input string, const string
  Parameters:
    src (string): (string) String in which the regex pattern occurences are to be counted
    regex (string): (string) RegEx pattern
  Returns: The number of occurrences of the `regex` pattern in the `src` string.

method matchRgx(src, regex, occurrence)
  Finds given `occurence` of `regex` pattern in `src` string. ___NB!___ _Does not support `^` (beginning of the string placeholder), lookbehind and some other complex patterns, because it works by cutting off the part of the string up to the first found occurence (inclusive) and then continuing the search on the remainder of the string. E.g. in a four line source `(?<=\n).+\n` should match the second and the third lines but matchRgx only matches the second line since after matching it continues to search in the remainder AFTER the match only. _
  Namespace types: series string, simple string, input string, const string
  Parameters:
    src (string): (string) String to search for the regex pattern
    regex (string): (string) RegEx pattern
    occurrence (int): (int) (Default is 1) The number of the occurrence to search for. If this params exceeds the actual
number of occurrences of the pattern in the `src` string the following tuple is returned [na, src length, na]
  Returns: [matchS , matchPos, matchL] A tuple, matchS - matched substring, matchPos - position of the match, matchL - match length


HOW TO USE

See DEMO section in the script.
You can test regex patterns by playing around with script input settings.
Another usage example can be found in my CSVParser and HTFMAs libraries.
CSVregexstringstrings

Pine脚本库

本着真正的TradingView精神,作者将此Pine代码发布为开源脚本库,以便我们社区的其他Pine程序员可以重复使用它。向作者致敬!您可以私下或在其它开源出版物中使用此脚本库,但在出版物中重复使用此代码受网站规则约束。

免责声明