Skip to main content

regexp

Availability

Device + Agent

Description

Squirrel’s regexp delegate object provides a series of methods which allow you to use and evaluate regular expressions within your Squirrel code.

Important Because of the known issues below and other factors, we strongly recommend that you implement regular expressions in your agent code using regexp2 as a drop-in replacement for regexp.

Known Issues

If you attempt to use match against strings that contain NUL characters (ie. bytes of value 0), then Squirrel can’t match against any characters beyond the string’s first NUL.

Greedy regular expressions are not in fact as greedy as they should be. For example, b+ should be a greedy match: for example, (b+)(bbb) should match bbbbbbb with the first group capturing bbbb, the second capturing bbb, and the match spanning the whole string. However, with Squirrel regexp not all of these are captured:

local es = "(b+)(bbb)"
local cs = "bbbbbbb";

local r = regexp(es);
local r2 = regexp2(es);

local rs = r.capture(cs);
local rs2 = r2.capture(cs);

if (rs) {
    server.log("regexp");
    foreach (index, value in rs) {
        server.log(format("  Match number %02d: %s", index + 1, cs.slice(value.begin, value.end)));
    }
}

if (rs2) {
    server.log("regexp2");
    foreach (index, value in rs2) {
        server.log(format("  Match number %02d: %s", index + 1, cs.slice(value.begin, value.end)));
    }
}

yields:

2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent] regexp
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent]   Match number 01: bbbb
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent]   Match number 02: b
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent]   Match number 03: bbb
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent] regexp2
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent]   Match number 01: bbbbbbb
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent]   Match number 02: bbbb
2019-07-22 10:40:29.181 +10:00 "imp004m-BB-3": [Agent]   Match number 03: bbb

Member Entities

The regexp object has the following member methods:

  • regexp.capture()Finds the first match against the target regular expression and tabulates group captures within the match
  • regexp.match()Determines whether the target regular expression matches the whole of the passed string
  • regexp.search()Finds the first sub-string within the passed string which matches the target regular expression

The regexp object has the following member function:

  • regexp()Instantiates a regular expression object