match_regex/5—match a string with a regular expression and retrieve a  given number of fields from it


The match_regex/5 function matches a string with a regular expression and retrieve a  given number of fields from it. The syntax for this function is as follows:

match_regex($STR,$REGEX,$OPTS,$FLDCNT,$FLDS)
$FLDS=match_regex($STR,$REGEX,$OPTS,$FLDCNT)

match_regex/5 arguments

Argument

Mode

Type

Description

$STR

Input

STRING

Specifies the string to be matched

$REGEX

Input

STRING

Specifies the regular expression to match to the string

$OPTS

Input

STRING

Specifies the options for how the regular expression engine operates

$FLDCNT

Input

INTEGER

Requested number of fields to be retrieved

$FLDS

Output

LIST_OF STRING

Retrieved fields

Use the match_regex/5 to match a string $STR with regular expression $REGEX applying options from $OPTS and to collect the first $FLDCNT retrieved fields in $FLDS.

Note

The regular expression $REGEX must be compliant with the Perl Compatible Regular Expression specification. For a specification, see http://perldoc.perl.org/perlre.html.

The options argument can be either an empty string or a sequence of any of the following option indicators:

Option

Description

i

Perform case insensitive string comparison

m

Multi-line mode

s

A dot matches any character, including new line

x

Extended mode

Note: In extended mode, blank space data characters in the pattern are ignored, except when they are escaped or inside a character class. Characters between an unescaped # outside a character class and the next new line character, inclusive, are also ignored.

The $FLDS argument can be specified as one variable that will get a list value, or it can be specified as a list of $FLDCNT variables.

match_regex/5 example

match_regex('2007 02 04 mcell: RULES: xyz',
'[0-9]* [0-9]* [0-9]* ([^:]*): ([^:]*): .*','',1,[$FLD]);

The input string, which could be part of a trace, matches the regular expression. There are two fields, but only the first one is collected in $FLD as mcell.

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*