string_split

Table of Contents

Prototype: string_split(string, regex, maxent)

Return type: slist

Description: Splits string into at most maxent substrings wherever regex occurs, and returns the list with those strings.

The regular expression is unanchored.

If the maximum number of substrings is insufficient to accommodate all the entries, the generated slist will have maxent items and the last one will contain the rest of the string starting with the maxent-1-th delimiter. This is standard behavior in many languages like Perl or Ruby, and different from the splitstring() behavior.

Arguments:

  • string: string, in the range: .*
  • regex: regular expression, in the range: .*
  • maxent: int, in the range: 0,99999999999

Example:

body common control
{
      bundlesequence => { "test" };
}

bundle agent test
{
  vars:

      "split1" slist => string_split("one:two:three", ":", "10");
      "split2" slist => string_split("one:two:three", ":", "1");
      "split3" slist => string_split("alpha:xyz:beta", "xyz", "10");

  reports:

      "split1: $(split1)";  # will list "one", "two", and "three"
      "split2: $(split2)";  # will list "one:two:three"
      "split3: $(split3)";  # will list "alpha:" and ":beta"

}

Output:

R: split1: one
R: split1: two
R: split1: three
R: split2: one:two:three
R: split3: alpha:
R: split3: :beta

History: Introduced in CFEngine 3.6; deprecates splitstring().

See also: splitstring()