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. |
* | Matches zero-or-more iterations of preceding pattern element. |
+ | Matches one-or-more iterations of preceding pattern element. |
? | Matches zero or one-time incidence of preceding pattern element. |
( ) | Groups subpatterns. Iteration or selection applies to entire subpatterns. |
| | Selection |
[ ] | Matches characters included in a set of characters. |
^ | Matches the beginning of a string when specified at the beginning of a pattern. |
$ | Matches the end of a character string when specified at the end of a pattern. |
Rules of Pattern Search
The first and longest string that matches a pattern is selected.
When " |" (selection) is included, the pattern search starts from the left side subpattern.
In expressions that use "*", "+", or "?", strings having the longest match take precedence.
For a sequence of two or more partial expressions, the pattern search starts from the left one.
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.
[, ], $, (, ), {, }
Use the special characters in either of the following manners:
Place a backslash sign (\) in front of the special character to use it as an escape character.
Enclose the entire regular expression by braces ({}).
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.
The following example checks the text for character "*":
regexp "\\*" $text
or
regexp {\*} $text
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.
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.
couldn't compile regular expression pattern: parentheses () not balanced
In the description of regular expression, parentheses "()" are not described correctly. Check if either opening parenthesis or closing parenthesis is missing. To use "()" as normal characters, escape them by putting backslash signs (\).
couldn't compile regular expression pattern: brackets [] not balanced
In the description of regular expression, brackets "[]" are not described correctly. Check if either opening bracket or closing bracket is missing. To use "[]" as normal characters, escape them by putting backslash signs (\).
couldn't compile regular expression pattern: invalid character range
The range of character set is not specified correctly. Check if there is an error in the description enclosed in "[]." When specifying the range of character set, specify it in ascending order such as [0-9] and [a-z].
couldn't compile regular expression pattern: quantifier operand invalid
Check if the use of regular expressions is correct.
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.