home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tclX-6.4 / help / intro / regexps < prev    next >
Encoding:
Text File  |  1992-12-17  |  4.2 KB  |  77 lines

  1.      REGULAR EXPRESSIONS
  2.           Tcl provides two commands that support string matching using
  3.           egrep-style regular expressions: regexp and regsub.  Regular
  4.           expressions are implemented using Henry  Spencer's  package,
  5.           and  the  description of regular expressions below is copied
  6.           verbatim from his manual entry.
  7.  
  8.           A regular expression is zero or more branches, separated  by
  9.           ``|''.    It  matches  anything  that  matches  one  of  the
  10.           branches.
  11.  
  12.           A branch is zero or more pieces, concatenated.  It matches a
  13.           match  for  the  first,  followed by a match for the second,
  14.           etc.
  15.  
  16.           A piece is an atom possibly followed  by  ``*'',  ``+'',  or
  17.           ``?''.  An atom followed by ``*'' matches a sequence of 0 or
  18.           more matches of the atom.  An atom followed by ``+'' matches
  19.           a  sequence  of  1  or  more  matches  of the atom.  An atom
  20.           followed by ``?'' matches a match of the atom, or  the  null
  21.           string.
  22.  
  23.           An atom is a regular expression in parentheses  (matching  a
  24.           match  for  the  regular  expression),  a range (see below),
  25.           ``.'' (matching any single character), ``^''  (matching  the
  26.           null  string  at  the  beginning of the input string), ``$''
  27.           (matching the null string at the end of the input string), a
  28.           ``\''   followed   by  a  single  character  (matching  that
  29.           character), or a single character with no other significance
  30.           (matching that character).
  31.  
  32.           A range is a sequence of characters enclosed in ``[]''.   It
  33.           normally matches any single character from the sequence.  If
  34.           the sequence  begins  with  ``^'',  it  matches  any  single
  35.           character  not  from  the  rest  of  the  sequence.   If two
  36.           characters in the sequence are separated by ``-'',  this  is
  37.           shorthand for the full list of ASCII characters between them
  38.           (e.g. ``[0-9]'' matches any decimal digit).   To  include  a
  39.           literal  ``]''  in the sequence, make it the first character
  40.           (following a possible ``^'').  To include a  literal  ``-'',
  41.           make it the first or last character.
  42.  
  43.           If a regular expression could match two different parts of a
  44.           string,  it  will  match  the one which begins earliest.  If
  45.           both begin in the same place but match different lengths, or
  46.           match  the same length in different ways, life gets messier,
  47.           as follows.
  48.  
  49.           In general, the possibilities in  a  list  of  branches  are
  50.           considered  in  left-to-right  order,  the possibilities for
  51.           ``*'', ``+'', and ``?'' are considered longest-first, nested
  52.           constructs   are  considered  from  the  outermost  in,  and
  53.           concatenated constructs are considered leftmost-first.   The
  54.           match  that will be chosen is the one that uses the earliest
  55.           possibility in the first choice that has  to  be  made.   If
  56.           there  is more than one choice, the next will be made in the
  57.           same manner (earliest possibility) subject to  the  decision
  58.           on the first choice.  And so forth.
  59.  
  60.           For example, ``(ab|a)b*c'' could match ``abc'' in one of two
  61.           ways.   The  first choice is between ``ab'' and ``a''; since
  62.           ``ab'' is earlier, and does lead  to  a  successful  overall
  63.           match, it is chosen.  Since the ``b'' is already spoken for,
  64.           the  ``b*''  must  match  its  last  possibility-the   empty
  65.           string-since it must respect the earlier choice.
  66.  
  67.           In the particular case where no ``|''s are present and there
  68.           is  only  one ``*'', ``+'', or ``?'', the net effect is that
  69.           the longest possible match  will  be  chosen.   So  ``ab*'',
  70.           presented with ``xabbbby'', will match ``abbbb''.  Note that
  71.           if ``ab*'' is tried against  ``xabyabbbz'',  it  will  match
  72.           ``ab''  just  after  ``x'', due to the begins-earliest rule.
  73.           (In effect, the decision on where to start the match is  the
  74.           first  choice  to  be  made,  hence  subsequent choices must
  75.           respect  it  even  if  this  leads  them  to  less-preferred
  76.           alternatives.)
  77.