RExtract
The RExtract function uses regular expressions to extract a substring from a string.
This function supports Perl 5 style regular expressions.
Syntax
The RExtract
function
has the following syntax:
String = RExtract(Expression, Pattern, Flag)
Parameters
The RExtract
function
has the following parameters.
Parameter |
Format |
Description |
---|---|---|
|
String |
String that contains the substring to extract. The data to extract is within () in the Expression. |
|
String |
Regular expression pattern that specifies the substring to extract. |
|
true or false |
The behavior of the function Set Set The default setting for this parameter is true. |
Return value
The extracted string.
Examples
Example 1
The following
example shows how to use the RExtract
function.
Log(RExtract("Not responding to ping on host DB_01", ".*\s(DB_01).*"));
This example prints the following message to the policy log:
Parser Log: DB_01
Example 2
The following example shows how to perform a case-insensitive match.
In the pattern parameter, prefix the pattern string with the case insensitive modifier:
(?i)
.
expression = 'application/json;charset=utf-8';
test = Log(RExtract(expression, '(?i)(APPLICATION/JSON)'));
Log(test);
This example prints the following message to the policy log:
[PolicyLogger][testRExtract][pool-4-thread-11]Parser log: application/json
If the expression contains multiple lines and you want the pattern to span across line
terminators, use the dotall modifier: (?s)
.
expression="Line1: IGNORE\r\nBEGIN Line2\r\nLine3\r\nLine4\r\nEND\r\nLine5: IGNORE";
Log(RExtract(expression, "(?s)BEGIN(.*)END")); // Prints nothing
This example returns a match spanning across several lines:
[PolicyLogger][4REXTRACTSAMPLES][pool-4-thread-11]Parser log: Line2
Line3
Line4
Example 3
This example shows legacy RExtract
behavior.
In the case of multiple matches, return the first one:
expression="<siteId>122</siteId><siteId>204</siteId><siteId>234</siteId>";
test = RExtract(expression, "<siteId\b[^>]*>(.*?)</siteId>",false);
testNum = length(test);
log ("Test Num= " + testNum);
log ("Site ID = " + test);
This example prints the following message to the policy log:
[PolicyLogger]: [testRExtract][pool-3-thread-9]Parser log: Test Num= 3
[PolicyLogger]: [testRExtract][pool-3-thread-9]Parser log: Site ID = 122
In the case of multiple matches, return the last one:
expression="<siteId>122</siteId><siteId>204</siteId><siteId>234</siteId>";
test = RExtract(expression, "<siteId\b[^>]*>(.*?)</siteId>",true);
testNum = length(test);
log ("Test Num= " + testNum);
log ("Site ID = " + test);
This example prints the following message to the policy log:
[PolicyLogger]: [testRExtract][pool-3-thread-9]Parser log: Test Num= 3
[PolicyLogger]: [testRExtract][pool-3-thread-9]Parser log: Site ID = 234
Example 4
This
example returns NULL
to RExtract, starting with Impact
7.1.0.3.
Log(RExtract("Not responding
to ping on host DB_01", ".*"));
This
will return and log NULL
.
Previously, the legacy
version of Impact would return the entire string. To return the entire
string, in Impact version 7.1.0.3 or later, set Flag = false
(the
non-default setting) as in the following example:
Log(RExtract("Not
responding to ping on host DB_01", ".*",false));
This will return and log the entire string: Not
responding to ping on host DB_01
.