home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / splash / regexp.doc < prev    next >
Text File  |  1993-01-15  |  4KB  |  66 lines

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