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