Appendix A

 

      Regular expressions and how to use them

     

    For many configuration entries which are used for searching of text patterns the system may use PERL compatible regular expressions known usually as Unix style regular expressions.

    Explanation of regular expression's syntax

    Any single character matches itself, unless it is a meta-character with a special meaning listed in the following table.
    A series of characters in the expression matches that series of characters in the target string. So the pattern "hello" entered as regular expression would match ``hello'' in the target string. Quite simple.

      Summary table of used metacharacters

      CharacterMeaning
      ^ start of line
      $ end of line
      . any character
      \ quote next character
      * match zero or more, similar to {0,}
      + match one or more, similar to {1,}
      ? match zero or one, similar to {0,1}
      {n} Match exactly n times
      {n,} Match at least n times
      {n,m} Match at least n but not more than m times
      [aeiou0-9] match a, e, i, o, u, and 0 thru 9 ;
      [^aeiou0-9] match anything but a, e, i, o, u, and 0 thru 9
      \w matches an alphanumeric character (including "_")
      \W a nonalphanumeric
      \d matches a numeric character
      \D a non-numeric
      \s matches any space (same as [ \t\n\r\f])
      \S a non space
      \1..\9 backreferences

    You can force characters that normally function as metacharacters to be interpreted literally by prefixing them with a ``\''. For example, "^" match beginning of string, but "\^" match character "^", "\\" match "\" and so on.

    Characters may be specified using a metacharacter syntax much like that used in C: ``\n'' matches a newline, ``\t'' a tab, ``\r'' a carriage return, ``\f'' a form feed, etc. More generally, \xnn, where nn is a string of hexadecimal digits, matches the character whose ASCII value is nn. If you need wide (UniCode) character code, you can use '\x{nnnn}', where 'nnnn' - one or more hexadedimal digits.

    You can specify a character class, by enclosing a list of characters in [], which will match any one character from the list. If the first character after the ``['' is ``^'', the class matches any character not in the list.

    Within a list, the ``-'' character is used to specify a range, so that a-z represents all characters between ``a'' and ``z'', inclusive. If you want ``-'' itself to be a member of a class, put it at the start or end of the list, or escape it with a backslash.

    The following all specify the same class of three characters: [-az], [az-], and [a\-z]. All are different from [a-z], which specifies a class containing twenty-six characters. If you want ']' you may place it at the start of list or escape it with a backslash.

    Examples of ranges: [\n-\x0D] match any of #10,#11,#12,#13. [\d-t] match any digit, '-' or 't'. []-a] match any char from ']'..'a'.

    Finally, the ``.'' metacharacter matches any character except ``\n'' (unless you use /s modifier).

    You can specify a series of alternatives for a pattern using ``|'' to separate them, so that fee|fie|foe will match any of ``fee'', ``fie'', or ``foe'' in the target string (as would f(e|i|o)e). The first alternative includes everything from the last pattern delimiter (``('', ``['', or the beginning of the pattern) up to the first ``|'', and the last alternative contains everything from the last ``|'' to the next pattern delimiter. For this reason, it's common practice to include alternatives in parentheses, to minimize confusion about where they start and end.

    Alternatives are evaluated from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching foo|foot against ``barefoot'', only the ``foo'' part will match, as that is the first alternative tried, and it successfully matches the target string. (This might not seem important, but it is important when you are capturing matched text using parentheses.)

    Also remember that ``|'' is interpreted as a literal within square brackets, so if you write [fee|fie|foe] you're really only matching [feio|].

    The bracketing construct ( ... ) may also be used for define r.e. subexpressions. Subexpressions are numbered based on the left to right order of their opening parenthesis. First subexpression has number '1'.

    You may use \w, \d and \s within character classes.

    \1 through \9 are interpreted as backreferences. \ matches previously matched subexpression #. For example: '(.)\1+' match 'aaaa' and 'cc'. '(.+)\1+' also match 'abab' and '123123'.

    By default, the ^ character means the beginning of the string, the $ character the end (or before the newline at the end) and perl does certain optimizations with the assumption that the string contains only one line. Embedded newlines will not be matched by ``^'' or ``$''.


© Copyright 2001-2005 DS&T s.r.o,
www.bizz.cz