Prototype: splitstring(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 rest of the un-split string is thrown away.

Arguments:

  • string: string - A data string - in the range: .*
  • regex: regular expression - Regex to split on - in the range: .*
  • maxent: int - Maximum number of pieces - in the range: 0,99999999999

Example:

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

bundle agent test
{
  vars:

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

  reports:

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

}

Output:

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

History: Deprecated in CFEngine 3.6 in favor of string_split

See also: string_split()