Pattern Matching
Used in the following commands: glob, info, lsearch, regexp, regsub, string match, switch.
Tcl provides two (2) different techniques:
- Glob (aka wild cards)
- Regular expressions
Glob
- Easier to use than the regular expression
- Works well on simple cases
- Used by glob, info and string match
- Used through the -glob option by lsearch, switch
* | Matches any sequence of zero or more characters |
? | Matches any single character |
[char] | Matches any single character in chars. A sequence of chars can be defined using the dash symbol (i.e. a-z, 0-9) |
\x | Matches the single special character x. Used to avoid ambiguity with the other symbols. |
Regular Expression
- More powerful technique for pattern matching
- Same syntax of
grep -e
,egrep
- Subset of the one implemented by Perl
- Used by regexp and regsub
- Used through the -regexp option by lsearch and switch
- The basic building block of a pattern is called atom
Atom Meaning . Matches any single character ^ Matches the null string at the beginning of the input string $ Matches the null string at the end of the input string \x Matches the character x [chars] Matches any single character in chars. If the first character is ^, then it matches any character that is NOT in the set. If the first character (possibly after ^) is ] then it is treated literally. If - is the first, it is treated literally. (regexp) Matches anything that matches the regexp. Used for grouping and for identifying pieces of the matching substring. Operator Meaning * Matches any sequence of zero or more matches of the preceding atom + Matches any sequence of one or more matches of the preceding atom ? Matches either the null string or a match of the preceding atom regexp1|regexp2 Matches anything that matches either regexp1 or regexp2