Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/regexp.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 6.4 KB
Line 
1'\"
2'\" Copyright (c) 1998 Sun Microsystems, Inc.
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: regexp.n,v 1.28 2007/12/13 15:22:33 dgp Exp $
8'\"
9.so man.macros
10.TH regexp n 8.3 Tcl "Tcl Built-In Commands"
11.BS
12'\" Note:  do not modify the .SH NAME line immediately below!
13.SH NAME
14regexp \- Match a regular expression against a string
15
16.SH SYNOPSIS
17\fBregexp \fR?\fIswitches\fR? \fIexp string \fR?\fImatchVar\fR? ?\fIsubMatchVar subMatchVar ...\fR?
18.BE
19
20.SH DESCRIPTION
21.PP
22Determines whether the regular expression \fIexp\fR matches part or
23all of \fIstring\fR and returns 1 if it does, 0 if it does not, unless
24\fB\-inline\fR is specified (see below).
25(Regular expression matching is described in the \fBre_syntax\fR
26reference page.)
27.LP
28If additional arguments are specified after \fIstring\fR then they
29are treated as the names of variables in which to return
30information about which part(s) of \fIstring\fR matched \fIexp\fR.
31\fIMatchVar\fR will be set to the range of \fIstring\fR that
32matched all of \fIexp\fR.  The first \fIsubMatchVar\fR will contain
33the characters in \fIstring\fR that matched the leftmost parenthesized
34subexpression within \fIexp\fR, the next \fIsubMatchVar\fR will
35contain the characters that matched the next parenthesized
36subexpression to the right in \fIexp\fR, and so on.
37.PP
38If the initial arguments to \fBregexp\fR start with \fB\-\fR then
39they are treated as switches.  The following switches are
40currently supported:
41.TP 15
42\fB\-about\fR
43Instead of attempting to match the regular expression, returns a list
44containing information about the regular expression.  The first
45element of the list is a subexpression count.  The second element is a
46list of property names that describe various attributes of the regular
47expression. This switch is primarily intended for debugging purposes.
48.TP 15
49\fB\-expanded\fR
50Enables use of the expanded regular expression syntax where
51whitespace and comments are ignored.  This is the same as specifying
52the \fB(?x)\fR embedded option (see the \fBre_syntax\fR manual page).
53.TP 15
54\fB\-indices\fR
55Changes what is stored in the \fIsubMatchVar\fRs.
56Instead of storing the matching characters from \fIstring\fR,
57each variable
58will contain a list of two decimal strings giving the indices
59in \fIstring\fR of the first and last characters in the matching
60range of characters.
61.TP 15
62\fB\-line\fR
63Enables newline-sensitive matching.  By default, newline is a
64completely ordinary character with no special meaning.  With this
65flag,
66.QW [^
67bracket expressions and
68.QW .
69never match newline,
70.QW ^
71matches an empty string after any newline in addition to its normal
72function, and
73.QW $
74matches an empty string before any newline in
75addition to its normal function.  This flag is equivalent to
76specifying both \fB\-linestop\fR and \fB\-lineanchor\fR, or the
77\fB(?n)\fR embedded option (see the \fBre_syntax\fR manual page).
78.TP 15
79\fB\-linestop\fR
80Changes the behavior of
81.QW [^
82bracket expressions and
83.QW .
84so that they
85stop at newlines.  This is the same as specifying the \fB(?p)\fR
86embedded option (see the \fBre_syntax\fR manual page).
87.TP 15
88\fB\-lineanchor\fR
89Changes the behavior of
90.QW ^
91and
92.QW $
93(the
94.QW anchors )
95so they match the
96beginning and end of a line respectively.  This is the same as
97specifying the \fB(?w)\fR embedded option (see the \fBre_syntax\fR
98manual page).
99.TP 15
100\fB\-nocase\fR
101Causes upper-case characters in \fIstring\fR to be treated as
102lower case during the matching process.
103.TP 15
104\fB\-all\fR
105Causes the regular expression to be matched as many times as possible
106in the string, returning the total number of matches found.  If this
107is specified with match variables, they will contain information for
108the last match only.
109.TP 15
110\fB\-inline\fR
111Causes the command to return, as a list, the data that would otherwise
112be placed in match variables.  When using \fB\-inline\fR,
113match variables may not be specified.  If used with \fB\-all\fR, the
114list will be concatenated at each iteration, such that a flat list is
115always returned.  For each match iteration, the command will append the
116overall match data, plus one element for each subexpression in the
117regular expression.  Examples are:
118.CS
119\fBregexp\fR -inline -- {\ew(\ew)} " inlined "
120      \fI\(-> in n\fR
121\fBregexp\fR -all -inline -- {\ew(\ew)} " inlined "
122      \fI\(-> in n li i ne e\fR
123.CE
124.TP 15
125\fB\-start\fR \fIindex\fR
126Specifies a character index offset into the string to start
127matching the regular expression at. 
128.VS 8.5
129The \fIindex\fR value is interpreted in the same manner
130as the \fIindex\fR argument to \fBstring index\fR.
131.VE 8.5
132When using this switch,
133.QW ^
134will not match the beginning of the line, and \eA will still
135match the start of the string at \fIindex\fR.  If \fB\-indices\fR
136is specified, the indices will be indexed starting from the
137absolute beginning of the input string.
138\fIindex\fR will be constrained to the bounds of the input string.
139.TP 15
140\fB\-\|\-\fR
141Marks the end of switches.  The argument following this one will
142be treated as \fIexp\fR even if it starts with a \fB\-\fR.
143.PP
144If there are more \fIsubMatchVar\fRs than parenthesized
145subexpressions within \fIexp\fR, or if a particular subexpression
146in \fIexp\fR does not match the string (e.g. because it was in a
147portion of the expression that was not matched), then the corresponding
148\fIsubMatchVar\fR will be set to
149.QW "\fB\-1 \-1\fR"
150if \fB\-indices\fR has been specified or to an empty string otherwise.
151.SH EXAMPLES
152Find the first occurrence of a word starting with \fBfoo\fR in a
153string that is not actually an instance of \fBfoobar\fR, and get the
154letters following it up to the end of the word into a variable:
155.CS
156\fBregexp\fR {\e<foo(?!bar\e>)(\ew*)} $string \-> restOfWord
157.CE
158Note that the whole matched substring has been placed in the variable
159\fB\->\fR which is a name chosen to look nice given that we are not
160actually interested in its contents.
161.PP
162Find the index of the word \fBbadger\fR (in any case) within a string
163and store that in the variable \fBlocation\fR:
164.CS
165\fBregexp\fR \-indices {(?i)\e<badger\e>} $string location
166.CE
167.PP
168Count the number of octal digits in a string:
169.CS
170\fBregexp\fR \-all {[0\-7]} $string
171.CE
172.PP
173List all words (consisting of all sequences of non-whitespace
174characters) in a string:
175.CS
176\fBregexp\fR \-all \-inline {\eS+} $string
177.CE
178
179.SH "SEE ALSO"
180re_syntax(n), regsub(n),
181.VS 8.5
182string(n)
183.VE
184
185
186.SH KEYWORDS
187match, regular expression, string
Note: See TracBrowser for help on using the repository browser.