The glob() function returns a TRUE or FALSE based on whether a pattern matches a string. The glob() function can be used as a conditional test for an if or while statement. The syntax is as follows:
glob(<pattern>, <string>)
The expression for a glob() function is a series of characters (or patterns) that are matched against incoming character strings. You use these expressions when you define matching criteria.
Matching is done strictly from left to right, one character or basic glob expression at a time. Characters that are not part of match constructs will match themselves. The pattern and the incoming string must match completely. For example, the pattern abcd does not match the input “abcde” or” abc”.
A compound glob pattern consists of one or more basic wildcard patterns separated by ampersand (&) or tilde (~) characters. A compound wildcard pattern is matched by attempting to match each of its component basic wildcard patterns against the entire input string.
The pattern used in the glob() function is not the same pattern used in the pattern block of an ASL script. The symbols that describe the pattern are described in Symbols for a glob pattern .
Symbol |
Description |
---|---|
* |
Matches an arbitrary string of characters. The string can be empty. |
? |
Matches any single character. |
^ |
Acts as a NOT. Use this in conjunction with other symbols or characters. |
[set] |
Matches:
A hyphen (-) within [set] indicates a range, so that [a-d] is equivalent to [abcd]. The character before the hyphen (-) must precede the character after it or the range will be empty. The character (^) in any position except the first, or a hyphen (-) at the first or last position, has no special meaning. |
<n1-n2> |
Matches numbers in a given range. Both n1 and n2 must be strings of digits, which represent non-negative integer values. The matching characters are a non-empty string of digits whose value, as a non-negative integer, is greater than or equal to n1 and less than or equal to n2. If either end of the range is omitted, no limitation is placed on the accepted number. |
| |
Matches alternatives. For example, ”ab|bc|cd” without spaces matches exactly the three following strings: “ab”, “bc”, and “cd”. A vertical bar (|) as the first or last character of a pattern accepts an empty string as a match. |
\ |
Removes the special status, if any, of the following character. Backslash (\) has no special meaning within a set ([set]) or range (<n1-n2>) construct. |
& |
“And Also” for a compound wildcard pattern. If a component basic wildcard pattern is preceded by & (or is the first basic wildcard pattern in the compound wildcard pattern), it must successfully match. |
~ |
“Except” for a compound wildcard pattern (opposite function of &). If a component basic wildcard pattern is preceded by ~, it must not match. |
Spaces are interpreted as characters and are subject to matching even if they are adjacent to operators like “&.”
If the first character of a compound wildcard expression is an ampersand (&) or tilde (~) character, the compound is interpreted as if an asterisk (*) appeared at the beginning of the pattern. For example, the following two expressions are equivalent to each other:
~*[0-9]* *~*[0-9]*
Both of these expressions match any string that does not contain any digits.
A trailing instance of an ampersand character (&) can match only the empty string. A trailing instance of a tilde character (~) can be read as “except for the empty string.”
The following script prints sentences that contain the characters “Ship” or “ship.”
ASL Script (glob_do.asl): START { sentence:rep(word) eol } do { if (glob("*[Ss]hip*",sentence)) {print(sentence);} } Input (glob_do.txt): I have a ship that floats. Shipping is a big industry in Hong Kong. The dog ate my homework. I fell down and hurt my hip. A boat is much smaller than a ship. Output: $ sm_adapter --file=glob_do.txt glob_do.asl I have a ship that floats. Shipping is a big industry in Hong Kong. A boat is much smaller than a ship. $