home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / doc / regexp.n < prev    next >
Text File  |  1993-08-03  |  7KB  |  161 lines

  1. '\"
  2. '\" Copyright (c) 1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/regexp.n,v 1.4 93/08/03 16:37:28 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS regexp tcl
  25. .BS
  26. '\" Note:  do not modify the .SH NAME line immediately below!
  27. .SH NAME
  28. regexp \- Match a regular expression against a string
  29. .SH SYNOPSIS
  30. \fBregexp \fR?\fIswitches\fR? \fIexp string \fR?\fImatchVar\fR? ?\fIsubMatchVar subMatchVar ...\fR?
  31. .BE
  32.  
  33. .SH DESCRIPTION
  34. .PP
  35. Determines whether the regular expression \fIexp\fR matches part or
  36. all of \fIstring\fR and returns 1 if it does, 0 if it doesn't.
  37. .LP
  38. If additional arguments are specified after \fIstring\fR then they
  39. are treated as the names of variables in which to return
  40. information about which part(s) of \fIstring\fR matched \fIexp\fR.
  41. \fIMatchVar\fR will be set to the range of \fIstring\fR that
  42. matched all of \fIexp\fR.  The first \fIsubMatchVar\fR will contain
  43. the characters in \fIstring\fR that matched the leftmost parenthesized
  44. subexpression within \fIexp\fR, the next \fIsubMatchVar\fR will
  45. contain the characters that matched the next parenthesized
  46. subexpression to the right in \fIexp\fR, and so on.
  47. .LP
  48. If the initial arguments to \fBregexp\fR start with \fB\-\fR then
  49. .VS
  50. they are treated as switches.  The following switches are
  51. currently supported:
  52. .TP 10
  53. \fB\-nocase\fR
  54. Causes upper-case characters in \fIstring\fR to be treated as
  55. lower case during the matching process.
  56. .TP 10
  57. \fB\-indices\fR
  58. Changes what is stored in the \fIsubMatchVar\fRs. 
  59. Instead of storing the matching characters from \fBstring\fR,
  60. each variable
  61. will contain a list of two decimal strings giving the indices
  62. in \fIstring\fR of the first and last characters in the matching
  63. range of characters.
  64. .TP 10
  65. \fB\-\|\-\fR
  66. Marks the end of switches.  The argument following this one will
  67. be treated as \fIexp\fR even if it starts with a \fB\-.
  68. .VE
  69. .LP
  70. If there are more \fIsubMatchVar\fR's than parenthesized
  71. subexpressions within \fIexp\fR, or if a particular subexpression
  72. in \fIexp\fR doesn't match the string (e.g. because it was in a
  73. portion of the expression that wasn't matched), then the corresponding
  74. \fIsubMatchVar\fR will be set to ``\fB\-1 \-1\fR'' if \fB\-indices\fR
  75. has been specified or to an empty string otherwise.
  76.  
  77. .SH "REGULAR EXPRESSIONS"
  78. .PP
  79. Regular expressions are implemented using Henry Spencer's package
  80. (thanks, Henry!),
  81. and much of the description of regular expressions below is copied verbatim
  82. from his manual entry.
  83. .PP
  84. A regular expression is zero or more \fIbranches\fR, separated by ``|''.
  85. It matches anything that matches one of the branches.
  86. .PP
  87. A branch is zero or more \fIpieces\fR, concatenated.
  88. It matches a match for the first, followed by a match for the second, etc.
  89. .PP
  90. A piece is an \fIatom\fR possibly followed by ``*'', ``+'', or ``?''.
  91. An atom followed by ``*'' matches a sequence of 0 or more matches of the atom.
  92. An atom followed by ``+'' matches a sequence of 1 or more matches of the atom.
  93. An atom followed by ``?'' matches a match of the atom, or the null string.
  94. .PP
  95. An atom is a regular expression in parentheses (matching a match for the
  96. regular expression), a \fIrange\fR (see below), ``.''
  97. (matching any single character), ``^'' (matching the null string at the
  98. beginning of the input string), ``$'' (matching the null string at the
  99. end of the input string), a ``\e'' followed by a single character (matching
  100. that character), or a single character with no other significance
  101. (matching that character).
  102. .PP
  103. A \fIrange\fR is a sequence of characters enclosed in ``[]''.
  104. It normally matches any single character from the sequence.
  105. If the sequence begins with ``^'',
  106. it matches any single character \fInot\fR from the rest of the sequence.
  107. If two characters in the sequence are separated by ``\-'', this is shorthand
  108. for the full list of ASCII characters between them
  109. (e.g. ``[0-9]'' matches any decimal digit).
  110. To include a literal ``]'' in the sequence, make it the first character
  111. (following a possible ``^'').
  112. To include a literal ``\-'', make it the first or last character.
  113.  
  114. .SH "CHOOSING AMONG ALTERNATIVE MATCHES"
  115. .PP
  116. In general there may be more than one way to match a regular expression
  117. to an input string.  For example, consider the command
  118. .DS
  119. \fBregexp  (a*)b*  aabaaabb  x  y
  120. .DE
  121. Considering only the rules given so far, \fBx\fR and \fBy\fR could
  122. end up with the values \fBaabb\fR and \fBaa\fR, \fBaaab\fR and \fBaaa\fR,
  123. \fBab\fR and \fBa\fR, or any of several other combinations.
  124. To resolve this potential ambiguity \fBregexp\fR chooses among
  125. alternatives using the rule ``first then longest''.
  126. In other words, it consders the possible matches in order working
  127. from left to right across the input string and the pattern, and it
  128. attempts to match longer pieces of the input string before shorter
  129. ones.  More specifically, the following rules apply in decreasing
  130. order of priority:
  131. .IP [1]
  132. If a regular expression could match two different parts of an input string
  133. then it will match the one that begins earliest.
  134. .IP [2]
  135. If a regular expression contains \fB|\fR operators then the leftmost
  136. matching sub-expression is chosen.
  137. .IP [3]
  138. In \fB*\fR, \fB+\fR, and \fB?\fR constructs, longer matches are chosen
  139. in preference to shorter ones.
  140. .IP [4]
  141. In sequences of expression components the components are considered
  142. from left to right.
  143. .LP
  144. In the example from above, \fB(a*)b*\fR matches \fBaab\fR:  the \fB(a*)\fR
  145. portion of the pattern is matched first and it consumes the leading
  146. \fBaa\fR; then the \fBb*\fR portion of the pattern consumes the
  147. next \fBb\fR.  Or, consider the following example:
  148. .DS
  149. \fBregexp  (ab|a)(b*)c  abc  x  y  z
  150. .DE
  151. After this command \fBx\fR will be \fBabc\fR, \fBy\fR will be
  152. \fBab\fR, and \fBz\fR will be an empty string.
  153. Rule 4 specifies that \fB(ab|a)\fR gets first shot at the input
  154. string and Rule 2 specifies that the \fBab\fR sub-expression
  155. is checked before the \fBa\fR sub-expression.
  156. Thus the \fBb\fR has already been claimed before the \fB(b*)\fR
  157. component is checked and \fB(b*)\fR must match an empty string.
  158.  
  159. .SH KEYWORDS
  160. match, regular expression, string
  161.