Prototype: regextract(regex, string, backref)

Return type: boolean

Description: Returns whether the anchored regex matches the string, and fills the array backref with back-references.

This function should be avoided in favor of data_regextract() because it creates classic CFEngine array variables and does not support named captures.

If there are any back reference matches from the regular expression, then the array will be populated with the values, in the manner:

code
$(backref[0]) = entire string
$(backref[1]) = back reference 1, etc

Arguments:

  • regex: regular expression - Regular expression - in the range: .*
  • string: string - Match string - in the range: .*
  • backref: string - Identifier for back-references - in the range: [a-zA-Z0-9_$(){}\[\].:]+

Example:

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

bundle agent example
{
  classes:

      # Extract regex backreferences and put them in an array

      "ok" expression => regextract(
                                     "xx ([^\s]+) ([^\s]+).* xx",
                                     "xx one two three four xx",
                                     "myarray"
      );
  reports:

    ok::

      "ok - \"$(myarray[0])\" = xx + \"$(myarray[1])\" + \"$(myarray[2])\" + .. + xx";
}

Output:

code
R: ok - "xx one two three four xx" = xx + "one" + "two" + .. + xx

See also: data_regextract() regex_replace()