Top
Systemwalker Operation Manager  Reference Guide
FUJITSU Software

15.1.4 Regular Expression

In a script, you can use regular expressions. The regular expressions used in scripts are explained below.

Symbols of Regular Expression

.

Matches any single character.
Example: regexp {ABC.} $text
Description: Checks the presence of the characters for the 4-character string containing ABC followed by any single character.

*

Matches zero-or-more iterations of preceding pattern element.
Example: regexp {<.*>} $text
Description: Checks the presence of the string for any element enclosed in <>. (The enclosed element may be a blank string.)

+

Matches one-or-more iterations of preceding pattern element.
Example: regexp {<.+>} $text
Description: Checks the presence of the string for any character enclosed by <>. (One or more characters must be included between < and >.)

?

Matches zero or one-time incidence of preceding pattern element.
Example: regexp {0?1} $text
Description: Checks the presence of the string for "1" or "01."

( )

Groups subpatterns. Iteration or selection applies to entire subpatterns.
Example: regexp {(.*),(.*)} $text all sub1 sub2
Description: Checks the presence of the string for commas and, if included, extracts all matches to all and extracts the characters before and after the commas to sub1 and sub2. (The characters that precede and follow commas can be blank strings.)

|

Selection
Example: regexp {Info|Warning} $text
Description: Checks the presence of the string for "Info" or "Warning."

[ ]

Matches characters included in a set of characters.
If the first character is "^", it matches a character not included in the set of characters. The range is specified using "-" (hyphen) (e.g. [a-z]).
Example: regexp {ABC[0-9][0-9][^ ]} $text
Description: Checks the presence of a 2-digit number following ABC and if a non-blank character follows right after it.

^

Matches the beginning of a string when specified at the beginning of a pattern.
Example: regexp{^ABC} $text
Description: Checks to see if the beginning of string is "ABC."

$

Matches the end of a character string when specified at the end of a pattern.
Example: regexp{ABC$} $text
Description: Checks to see if the end of string is "ABC."

Rules of Pattern Search

Handling of Special Characters and Regular Expressions

Some of the regular expression symbols correspond to the special characters used in scripts. To use them, write them as shown below.

Special characters used in scripts

[, ], $, (, ), {, }

Use of special characters

Use the special characters in either of the following manners:

Example:

The following example specifies regular expression "[0-9]" in the argument of regexp.

regexp "\[0-9\]" $text

or

regexp {[0-9]} $text

To treat a regular expression symbol as an ordinary character, place a backslahs (\) in front of it. Note that \ is also a special character in scripts and thus must be written "\\" or the entire regular expression must be enclosed in braces.

Example 1:

The following example checks the text for character "*":

regexp "\\*" $text

or

regexp {\*} $text
Example 2:

The following example checks the text for character "[":

regexp "\\\[" $text

or

regexp {\[} $text

To treat the backslash sign itself in a regular expression as an ordinary character, it must be written \\. Note that since \ is also a special character in scripts, either backslash signs must be written \\ or the entire regular expression must be enclosed in braces.

Example 3:

The following example checks the text for character "\":

regexp "\\\\" $text

or

regexp {\\} $text

Information

Escaping the scripts' special characters using the backslash signs requires multiple backslash signs as illustrated in Examples 2 and 3 and it may cause input errors. Thus, it is recommended that the regular expressions be always enclosed in braces.

Point

To extract strings, describe them using [^].

The following example extracts "BC" enclosed in "<>" to variable Var0 from the string "A<BC>D."

regexp {<([^>]*)} "A<BC>D" All Var0

The following example shows escaping of "BC" enclosed in "[]" to be extracted from the string "A[BC]D" by putting backslash signs in front of the special characters "[]":

regexp {\[([^\]]*)} "A\[BC\]D" All Var0

Typical Syntax Error in Regular Expression

The following shows typical syntax errors in regular expressions and corrective measures against them.

Caution

If there is an unwanted blank space in regular expression, check cannot be conducted properly since a blank space is identified as a single character in regular expression.