Description
Matches a regular expression against a string and extracts matched parts.
Synopsis
regexp exp string[matchvar] [submatchvar1 submatchvar2 ... ]
Options
Specify a regular expression.
Specify the string to check.
Specify the name of the variable in which parts of string that matched the regular expression are extracted and stored. If omitted, the matching parts will not be stored.
Specify the name of the variable in which parts of string that matched the parenthesized substrings are extracted and stored. If omitted, the matching parts will not be stored.
Point
A regular expression is a shorthand for describing the structure of a string using symbols. This command checks whether the string specified in the string option has a part that matches the structure indicated in regular expression exp.
If it does, the command extracts it from string and stores it in the matchvar variable. In regular expressions, you can specify the strings to extract as many as you want at any desired position. These strings will be stored in submatchvar variables in order.
For how to write a regular expression, see "15.1.4 Regular Expression".
Return Values
0: | Not matched. |
1: | Matched. |
Examples
Extracting before and after a specified character
The following example checks whether the string contains commas "," and, if contained, extracts all matches to all and extracts those characters preceding and following a comma to variables sub1 and sub2, respectively. (The characters that precede and follow a comma can be blank characters.)
regexp {(.*),(.*)} $text all sub1 sub2
Specifying a part to be extracted
The following example extracts the part enclosed in "<>" from the following message and stores it to variable Var0.
A "LinkUp" has occurred on the network. <An error has occurred with the DB server.>
regexp {<([^>]*)} $EventText All Var0
Message part "An error has occurred with the DB server." is stored to variable Var0.
Escaping special characters using backslash sign
The following example extracts the part after JobName= to the next blank, the bracketed part after RC= and the parenthesized part after device=, and then stores them to Var0, Var1 and Var2 respectively.
JOBNET Error. JobName=SCRJB_1 RC=[0022] xxx-device=(DX038)
regexp {JobName=([^ ]*) RC=\[([^\]]*)\] .*device=\(([^\)]*)\)} $EventText All Var0 Var1 Var2
"SCRJB_1", "0022" and "DX038" are stored to Var0, Var1 and Var2 respectively.