patterns

Patterns are used in triggers, hooks, /purge, /list, and /recall. There are three styles of pattern matching available: "simple" comparison, "glob" (similar to shell filename patterns), and "regexp" (regular expressions). The style used by a particular command is determined either by the use of the -m option or the setting of the global variable %{matching}.

Simple matching ("simple")

The pattern is compared directly to the string. There are no special characters. Case is significant.

Globbing ("glob")

Globbing is the default matching style, and was the only style available before version 3.2. It is similar to filename expansion ("globbing") used by many shells (but is only used for comparison, not expansion).

There are several special sequences that can be used in tf globbing:

Examples:
"d?g" matches "dog", "dig" and "dug" but not "dg" or "drug".
"d*g" matches "dg", "dog", "drug", "debug", "dead slug", etc.
"{d*g}" matches "dg", "dog", "drug", "debug", but not "dead slug".
"M[rs]." matches "Mr." and "Ms."
"M[a-z]" matches "Ma", "Mb", "Mc", etc.
"[^a-z]" matches any character that is not in the English alphabet.
"{storm|chup*}*" matches "chupchup fehs" and "Storm jiggles".
"{storm|chup*}*" does NOT match "stormette jiggles".

Regular expressions ("regexp")

The regexp package was written by Henry Spencer, and is similar to those used in egrep and many text editors. See also: regmatch(), substitution. The following excerpt is taken from Henry Spencer's regexp(3) man page.

     REGULAR EXPRESSION SYNTAX
          A regular expression is zero or more branches, separated by
          `|'.  It matches anything that matches one of the branches.

          A branch is zero or more pieces, concatenated.  It matches a
          match for the first, followed by a match for the second,
          etc.

          A piece is an atom possibly followed by `*', `+', or `?'.
          An atom followed by `*' matches a sequence of 0 or more
          matches of the atom.  An atom followed by `+' matches a
          sequence of 1 or more matches of the atom.  An atom followed
          by `?' matches a match of the atom, or the null string.

          An atom is a regular expression in parentheses (matching a
          match for the regular expression), a range (see below), `.'
          (matching any single character), `^' (matching the null
          string at the beginning of the input string), `$' (matching
          the null string at the end of the input string), a `\'
          followed by a single character (matching that character), or
          a single character with no other significance (matching that
          character).

          A range is a sequence of characters enclosed in `[]'.  It
          normally matches any single character from the sequence.  If
          the sequence begins with `^', it matches any single
          character not from the rest of the sequence.  If two
          characters in the sequence are separated by `-', this is
          shorthand for the full list of ASCII characters between them
          (e.g. `[0-9]' matches any decimal digit).  To include a
          literal `]' in the sequence, make it the first character
          (following a possible `^').  To include a literal `-', make
          it the first or last character.

     AMBIGUITY
          If a regular expression could match two different parts of
          the input string, it will match the one which begins
          earliest.  If both begin in the same place    but match
          different lengths, or match the same length in different
          ways, life gets messier, as follows.

          In general, the possibilities in a list of branches are
          considered in left-to-right order, the possibilities for
          `*', `+', and `?' are considered longest-first, nested
          constructs are considered from the outermost in, and
          concatenated constructs are considered leftmost-first.  The
          match that will be chosen is the one that uses the earliest
          possibility in the first choice that has to be made.  If
          there is more than one choice, the next will be made in the
          same manner (earliest possibility) subject to the decision
          on the first choice.  And so forth.

          For example, `(ab|a)b*c' could match `abc' in one of two
          ways.  The first choice is between `ab' and `a'; since `ab'
          is earlier, and does lead to a successful overall match, it
          is chosen.  Since the `b' is already spoken for, the `b*'
          must match its last possibility-the empty string-since it
          must respect the earlier choice.

          In the particular case where no `|'s are present and there
          is only one `*', `+', or `?', the net effect is that the
          longest possible match will be chosen.  So `ab*', presented
          with `xabbbby', will match `abbbb'.  Note that if `ab*' is
          tried against `xabyabbbz', it will match `ab' just after
          `x', due to the begins-earliest rule.  (In effect, the
          decision on where to start the match is the first choice to
          be made, hence subsequent choices must respect it even if
          this leads them to less-preferred alternatives.)

Comparison of glob and regexps.

In a glob, '*' and '?' by themselves match text. In a regexp, '*' and '?' are only meaningful in combination with the pattern they follow. Regexps are not "anchored"; that is, the match may occur anywhere in the string, unless you explicitly use '^' and/or '$' to anchor it. Globs are anchored, and must match the entire string.
    regexp		equivalent glob
			(except for case)
    ------		-----------------
    "part of line"	"*part of line*"
    "^entire line$"	"entire line"
    "(^| )word( |$)"	"*{word}*"
    "^(You|Hawkeye) "	"{You|Hawkeye} *"
    "foo.*bar"		"*foo*bar*"
    "f(oo|00)d"		"*{*food*|*f00d*}*"
    "line[0-9]"		"*line[0-9]*"
    "^[^ ]+ whispers,"	"{*} whispers,*"
    "foo(AB)?bar"	"*{*foobar*|*fooABbar*}*"
    "zoo+m"		none
    "foo ?bar"		none
    "(foo bar|frodo)"	none

Notes.


Back to index
Back to tf home page
Copyright © 1995 - 1999 Ken Keys