| 1 |
'\" |
| 2 |
'\" Copyright (c) 1998 Sun Microsystems, Inc. |
| 3 |
'\" Copyright (c) 1999 Scriptics Corporation |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.so man.macros |
| 9 |
.TH re_syntax n "8.1" Tcl "Tcl Built-In Commands" |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
re_syntax \- Syntax of Tcl regular expressions |
| 13 |
.BE |
| 14 |
.SH DESCRIPTION |
| 15 |
.PP |
| 16 |
A \fIregular expression\fR describes strings of characters. |
| 17 |
It's a pattern that matches certain strings and does not match others. |
| 18 |
.SH "DIFFERENT FLAVORS OF REs" |
| 19 |
Regular expressions |
| 20 |
.PQ RE s , |
| 21 |
as defined by POSIX, come in two flavors: \fIextended\fR REs |
| 22 |
.PQ ERE s |
| 23 |
and \fIbasic\fR REs |
| 24 |
.PQ BRE s . |
| 25 |
EREs are roughly those of the traditional \fIegrep\fR, while BREs are |
| 26 |
roughly those of the traditional \fIed\fR. This implementation adds |
| 27 |
a third flavor, \fIadvanced\fR REs |
| 28 |
.PQ ARE s , |
| 29 |
basically EREs with some significant extensions. |
| 30 |
.PP |
| 31 |
This manual page primarily describes AREs. BREs mostly exist for |
| 32 |
backward compatibility in some old programs; they will be discussed at |
| 33 |
the end. POSIX EREs are almost an exact subset of AREs. Features of |
| 34 |
AREs that are not present in EREs will be indicated. |
| 35 |
.SH "REGULAR EXPRESSION SYNTAX" |
| 36 |
.PP |
| 37 |
Tcl regular expressions are implemented using the package written by |
| 38 |
Henry Spencer, based on the 1003.2 spec and some (not quite all) of |
| 39 |
the Perl5 extensions (thanks, Henry!). Much of the description of |
| 40 |
regular expressions below is copied verbatim from his manual entry. |
| 41 |
.PP |
| 42 |
An ARE is one or more \fIbranches\fR, |
| 43 |
separated by |
| 44 |
.QW \fB|\fR , |
| 45 |
matching anything that matches any of the branches. |
| 46 |
.PP |
| 47 |
A branch is zero or more \fIconstraints\fR or \fIquantified atoms\fR, |
| 48 |
concatenated. |
| 49 |
It matches a match for the first, followed by a match for the second, etc; |
| 50 |
an empty branch matches the empty string. |
| 51 |
.SS QUANTIFIERS |
| 52 |
A quantified atom is an \fIatom\fR possibly followed |
| 53 |
by a single \fIquantifier\fR. |
| 54 |
Without a quantifier, it matches a single match for the atom. |
| 55 |
The quantifiers, |
| 56 |
and what a so-quantified atom matches, are: |
| 57 |
.RS 2 |
| 58 |
.TP 6 |
| 59 |
\fB*\fR |
| 60 |
. |
| 61 |
a sequence of 0 or more matches of the atom |
| 62 |
.TP |
| 63 |
\fB+\fR |
| 64 |
. |
| 65 |
a sequence of 1 or more matches of the atom |
| 66 |
.TP |
| 67 |
\fB?\fR |
| 68 |
. |
| 69 |
a sequence of 0 or 1 matches of the atom |
| 70 |
.TP |
| 71 |
\fB{\fIm\fB}\fR |
| 72 |
. |
| 73 |
a sequence of exactly \fIm\fR matches of the atom |
| 74 |
.TP |
| 75 |
\fB{\fIm\fB,}\fR |
| 76 |
. |
| 77 |
a sequence of \fIm\fR or more matches of the atom |
| 78 |
.TP |
| 79 |
\fB{\fIm\fB,\fIn\fB}\fR |
| 80 |
. |
| 81 |
a sequence of \fIm\fR through \fIn\fR (inclusive) matches of the atom; |
| 82 |
\fIm\fR may not exceed \fIn\fR |
| 83 |
.TP |
| 84 |
\fB*? +? ?? {\fIm\fB}? {\fIm\fB,}? {\fIm\fB,\fIn\fB}?\fR |
| 85 |
. |
| 86 |
\fInon-greedy\fR quantifiers, which match the same possibilities, |
| 87 |
but prefer the smallest number rather than the largest number |
| 88 |
of matches (see \fBMATCHING\fR) |
| 89 |
.RE |
| 90 |
.PP |
| 91 |
The forms using \fB{\fR and \fB}\fR are known as \fIbound\fRs. The |
| 92 |
numbers \fIm\fR and \fIn\fR are unsigned decimal integers with |
| 93 |
permissible values from 0 to 255 inclusive. |
| 94 |
.SS ATOMS |
| 95 |
An atom is one of: |
| 96 |
.RS 2 |
| 97 |
.IP \fB(\fIre\fB)\fR 6 |
| 98 |
matches a match for \fIre\fR (\fIre\fR is any regular expression) with |
| 99 |
the match noted for possible reporting |
| 100 |
.IP \fB(?:\fIre\fB)\fR |
| 101 |
as previous, but does no reporting (a |
| 102 |
.QW non-capturing |
| 103 |
set of parentheses) |
| 104 |
.IP \fB()\fR |
| 105 |
matches an empty string, noted for possible reporting |
| 106 |
.IP \fB(?:)\fR |
| 107 |
matches an empty string, without reporting |
| 108 |
.IP \fB[\fIchars\fB]\fR |
| 109 |
a \fIbracket expression\fR, matching any one of the \fIchars\fR (see |
| 110 |
\fBBRACKET EXPRESSIONS\fR for more detail) |
| 111 |
.IP \fB.\fR |
| 112 |
matches any single character |
| 113 |
.IP \fB\e\fIk\fR |
| 114 |
matches the non-alphanumeric character \fIk\fR |
| 115 |
taken as an ordinary character, e.g. \fB\e\e\fR matches a backslash |
| 116 |
character |
| 117 |
.IP \fB\e\fIc\fR |
| 118 |
where \fIc\fR is alphanumeric (possibly followed by other characters), |
| 119 |
an \fIescape\fR (AREs only), see \fBESCAPES\fR below |
| 120 |
.IP \fB{\fR |
| 121 |
when followed by a character other than a digit, matches the |
| 122 |
left-brace character |
| 123 |
.QW \fB{\fR ; |
| 124 |
when followed by a digit, it is the beginning of a \fIbound\fR (see above) |
| 125 |
.IP \fIx\fR |
| 126 |
where \fIx\fR is a single character with no other significance, |
| 127 |
matches that character. |
| 128 |
.RE |
| 129 |
.SS CONSTRAINTS |
| 130 |
A \fIconstraint\fR matches an empty string when specific conditions |
| 131 |
are met. A constraint may not be followed by a quantifier. The |
| 132 |
simple constraints are as follows; some more constraints are described |
| 133 |
later, under \fBESCAPES\fR. |
| 134 |
.RS 2 |
| 135 |
.TP 8 |
| 136 |
\fB^\fR |
| 137 |
. |
| 138 |
matches at the beginning of the string or a line (according to whether |
| 139 |
matching is newline-sensitive or not, as described in \fBMATCHING\fR, |
| 140 |
below). |
| 141 |
.TP |
| 142 |
\fB$\fR |
| 143 |
. |
| 144 |
matches at the end of the string or a line (according to whether |
| 145 |
matching is newline-sensitive or not, as described in \fBMATCHING\fR, |
| 146 |
below). |
| 147 |
.RS |
| 148 |
.PP |
| 149 |
The difference between string and line matching modes is immaterial |
| 150 |
when the string does not contain a newline character. The \fB\eA\fR |
| 151 |
and \fB\eZ\fR constraint escapes have a similar purpose but are |
| 152 |
always constraints for the overall string. |
| 153 |
.PP |
| 154 |
The default newline-sensitivity depends on the command that uses the |
| 155 |
regular expression, and can be overridden as described in |
| 156 |
\fBMETASYNTAX\fR, below. |
| 157 |
.RE |
| 158 |
.TP |
| 159 |
\fB(?=\fIre\fB)\fR |
| 160 |
. |
| 161 |
\fIpositive lookahead\fR (AREs only), matches at any point where a |
| 162 |
substring matching \fIre\fR begins |
| 163 |
.TP |
| 164 |
\fB(?!\fIre\fB)\fR |
| 165 |
. |
| 166 |
\fInegative lookahead\fR (AREs only), matches at any point where no |
| 167 |
substring matching \fIre\fR begins |
| 168 |
.RE |
| 169 |
.PP |
| 170 |
The lookahead constraints may not contain back references (see later), |
| 171 |
and all parentheses within them are considered non-capturing. |
| 172 |
.PP |
| 173 |
An RE may not end with |
| 174 |
.QW \fB\e\fR . |
| 175 |
.SH "BRACKET EXPRESSIONS" |
| 176 |
A \fIbracket expression\fR is a list of characters enclosed in |
| 177 |
.QW \fB[\|]\fR . |
| 178 |
It normally matches any single character from the list |
| 179 |
(but see below). If the list begins with |
| 180 |
.QW \fB^\fR , |
| 181 |
it matches any single character (but see below) \fInot\fR from the |
| 182 |
rest of the list. |
| 183 |
.PP |
| 184 |
If two characters in the list are separated by |
| 185 |
.QW \fB\-\fR , |
| 186 |
this is shorthand for the full \fIrange\fR of characters between those two |
| 187 |
(inclusive) in the collating sequence, e.g. |
| 188 |
.QW \fB[0\-9]\fR |
| 189 |
in Unicode matches any conventional decimal digit. Two ranges may not share an |
| 190 |
endpoint, so e.g. |
| 191 |
.QW \fBa\-c\-e\fR |
| 192 |
is illegal. Ranges in Tcl always use the |
| 193 |
Unicode collating sequence, but other programs may use other collating |
| 194 |
sequences and this can be a source of incompatibility between programs. |
| 195 |
.PP |
| 196 |
To include a literal \fB]\fR or \fB\-\fR in the list, the simplest |
| 197 |
method is to enclose it in \fB[.\fR and \fB.]\fR to make it a |
| 198 |
collating element (see below). Alternatively, make it the first |
| 199 |
character (following a possible |
| 200 |
.QW \fB^\fR ), |
| 201 |
or (AREs only) precede it with |
| 202 |
.QW \fB\e\fR . |
| 203 |
Alternatively, for |
| 204 |
.QW \fB\-\fR , |
| 205 |
make it the last character, or the second endpoint of a range. To use |
| 206 |
a literal \fB\-\fR as the first endpoint of a range, make it a |
| 207 |
collating element or (AREs only) precede it with |
| 208 |
.QW \fB\e\fR . |
| 209 |
With the exception of |
| 210 |
these, some combinations using \fB[\fR (see next paragraphs), and |
| 211 |
escapes, all other special characters lose their special significance |
| 212 |
within a bracket expression. |
| 213 |
.SS "CHARACTER CLASSES" |
| 214 |
Within a bracket expression, the name of a \fIcharacter class\fR |
| 215 |
enclosed in \fB[:\fR and \fB:]\fR stands for the list of all |
| 216 |
characters (not all collating elements!) belonging to that class. |
| 217 |
Standard character classes are: |
| 218 |
.IP \fBalpha\fR 8 |
| 219 |
A letter. |
| 220 |
.IP \fBupper\fR 8 |
| 221 |
An upper-case letter. |
| 222 |
.IP \fBlower\fR 8 |
| 223 |
A lower-case letter. |
| 224 |
.IP \fBdigit\fR 8 |
| 225 |
A decimal digit. |
| 226 |
.IP \fBxdigit\fR 8 |
| 227 |
A hexadecimal digit. |
| 228 |
.IP \fBalnum\fR 8 |
| 229 |
An alphanumeric (letter or digit). |
| 230 |
.IP \fBprint\fR 8 |
| 231 |
A "printable" (same as graph, except also including space). |
| 232 |
.IP \fBblank\fR 8 |
| 233 |
A space or tab character. |
| 234 |
.IP \fBspace\fR 8 |
| 235 |
A character producing white space in displayed text. |
| 236 |
.IP \fBpunct\fR 8 |
| 237 |
A punctuation character. |
| 238 |
.IP \fBgraph\fR 8 |
| 239 |
A character with a visible representation (includes both \fBalnum\fR |
| 240 |
and \fBpunct\fR). |
| 241 |
.IP \fBcntrl\fR 8 |
| 242 |
A control character. |
| 243 |
.PP |
| 244 |
A locale may provide others. A character class may not be used as an endpoint |
| 245 |
of a range. |
| 246 |
.RS |
| 247 |
.PP |
| 248 |
(\fINote:\fR the current Tcl implementation has only one locale, the Unicode |
| 249 |
locale, which supports exactly the above classes.) |
| 250 |
.RE |
| 251 |
.SS "BRACKETED CONSTRAINTS" |
| 252 |
There are two special cases of bracket expressions: the bracket |
| 253 |
expressions |
| 254 |
.QW \fB[[:<:]]\fR |
| 255 |
and |
| 256 |
.QW \fB[[:>:]]\fR |
| 257 |
are constraints, matching empty strings at the beginning and end of a word |
| 258 |
respectively. |
| 259 |
.\" note, discussion of escapes below references this definition of word |
| 260 |
A word is defined as a sequence of word characters that is neither preceded |
| 261 |
nor followed by word characters. A word character is an \fIalnum\fR character |
| 262 |
or an underscore |
| 263 |
.PQ \fB_\fR "" . |
| 264 |
These special bracket expressions are deprecated; users of AREs should use |
| 265 |
constraint escapes instead (see below). |
| 266 |
.SS "COLLATING ELEMENTS" |
| 267 |
Within a bracket expression, a collating element (a character, a |
| 268 |
multi-character sequence that collates as if it were a single |
| 269 |
character, or a collating-sequence name for either) enclosed in |
| 270 |
\fB[.\fR and \fB.]\fR stands for the sequence of characters of that |
| 271 |
collating element. The sequence is a single element of the bracket |
| 272 |
expression's list. A bracket expression in a locale that has |
| 273 |
multi-character collating elements can thus match more than one |
| 274 |
character. So (insidiously), a bracket expression that starts with |
| 275 |
\fB^\fR can match multi-character collating elements even if none of |
| 276 |
them appear in the bracket expression! |
| 277 |
.RS |
| 278 |
.PP |
| 279 |
(\fINote:\fR Tcl has no multi-character collating elements. This information |
| 280 |
is only for illustration.) |
| 281 |
.RE |
| 282 |
.PP |
| 283 |
For example, assume the collating sequence includes a \fBch\fR multi-character |
| 284 |
collating element. Then the RE |
| 285 |
.QW \fB[[.ch.]]*c\fR |
| 286 |
(zero or more |
| 287 |
.QW \fBch\fRs |
| 288 |
followed by |
| 289 |
.QW \fBc\fR ) |
| 290 |
matches the first five characters of |
| 291 |
.QW \fBchchcc\fR . |
| 292 |
Also, the RE |
| 293 |
.QW \fB[^c]b\fR |
| 294 |
matches all of |
| 295 |
.QW \fBchb\fR |
| 296 |
(because |
| 297 |
.QW \fB[^c]\fR |
| 298 |
matches the multi-character |
| 299 |
.QW \fBch\fR ). |
| 300 |
.SS "EQUIVALENCE CLASSES" |
| 301 |
Within a bracket expression, a collating element enclosed in \fB[=\fR |
| 302 |
and \fB=]\fR is an equivalence class, standing for the sequences of |
| 303 |
characters of all collating elements equivalent to that one, including |
| 304 |
itself. (If there are no other equivalent collating elements, the |
| 305 |
treatment is as if the enclosing delimiters were |
| 306 |
.QW \fB[.\fR \& |
| 307 |
and |
| 308 |
.QW \fB.]\fR .) |
| 309 |
For example, if \fBo\fR and \fB\(^o\fR are the members of an |
| 310 |
equivalence class, then |
| 311 |
.QW \fB[[=o=]]\fR , |
| 312 |
.QW \fB[[=\(^o=]]\fR , |
| 313 |
and |
| 314 |
.QW \fB[o\(^o]\fR \& |
| 315 |
are all synonymous. An equivalence class may not be an endpoint of a range. |
| 316 |
.RS |
| 317 |
.PP |
| 318 |
(\fINote:\fR Tcl implements only the Unicode locale. It does not define any |
| 319 |
equivalence classes. The examples above are just illustrations.) |
| 320 |
.RE |
| 321 |
.SH ESCAPES |
| 322 |
Escapes (AREs only), which begin with a \fB\e\fR followed by an |
| 323 |
alphanumeric character, come in several varieties: character entry, |
| 324 |
class shorthands, constraint escapes, and back references. A \fB\e\fR |
| 325 |
followed by an alphanumeric character but not constituting a valid |
| 326 |
escape is illegal in AREs. In EREs, there are no escapes: outside a |
| 327 |
bracket expression, a \fB\e\fR followed by an alphanumeric character |
| 328 |
merely stands for that character as an ordinary character, and inside |
| 329 |
a bracket expression, \fB\e\fR is an ordinary character. (The latter |
| 330 |
is the one actual incompatibility between EREs and AREs.) |
| 331 |
.SS "CHARACTER-ENTRY ESCAPES" |
| 332 |
Character-entry escapes (AREs only) exist to make it easier to specify |
| 333 |
non-printing and otherwise inconvenient characters in REs: |
| 334 |
.RS 2 |
| 335 |
.TP 5 |
| 336 |
\fB\ea\fR |
| 337 |
. |
| 338 |
alert (bell) character, as in C |
| 339 |
.TP |
| 340 |
\fB\eb\fR |
| 341 |
. |
| 342 |
backspace, as in C |
| 343 |
.TP |
| 344 |
\fB\eB\fR |
| 345 |
. |
| 346 |
synonym for \fB\e\fR to help reduce backslash doubling in some |
| 347 |
applications where there are multiple levels of backslash processing |
| 348 |
.TP |
| 349 |
\fB\ec\fIX\fR |
| 350 |
. |
| 351 |
(where \fIX\fR is any character) the character whose low-order 5 bits |
| 352 |
are the same as those of \fIX\fR, and whose other bits are all zero |
| 353 |
.TP |
| 354 |
\fB\ee\fR |
| 355 |
. |
| 356 |
the character whose collating-sequence name is |
| 357 |
.QW \fBESC\fR , |
| 358 |
or failing that, the character with octal value 033 |
| 359 |
.TP |
| 360 |
\fB\ef\fR |
| 361 |
. |
| 362 |
formfeed, as in C |
| 363 |
.TP |
| 364 |
\fB\en\fR |
| 365 |
. |
| 366 |
newline, as in C |
| 367 |
.TP |
| 368 |
\fB\er\fR |
| 369 |
. |
| 370 |
carriage return, as in C |
| 371 |
.TP |
| 372 |
\fB\et\fR |
| 373 |
. |
| 374 |
horizontal tab, as in C |
| 375 |
.TP |
| 376 |
\fB\eu\fIwxyz\fR |
| 377 |
. |
| 378 |
(where \fIwxyz\fR is one up to four hexadecimal digits) the Unicode |
| 379 |
character \fBU+\fIwxyz\fR in the local byte ordering |
| 380 |
.TP |
| 381 |
\fB\eU\fIstuvwxyz\fR |
| 382 |
. |
| 383 |
(where \fIstuvwxyz\fR is one up to eight hexadecimal digits) reserved |
| 384 |
for a Unicode extension up to 21 bits. The digits are parsed until the |
| 385 |
first non-hexadecimal character is encountered, the maximun of eight |
| 386 |
hexadecimal digits are reached, or an overflow would occur in the maximum |
| 387 |
value of \fBU+\fI10ffff\fR. |
| 388 |
.TP |
| 389 |
\fB\ev\fR |
| 390 |
. |
| 391 |
vertical tab, as in C are all available. |
| 392 |
.TP |
| 393 |
\fB\ex\fIhh\fR |
| 394 |
. |
| 395 |
(where \fIhh\fR is one or two hexadecimal digits) the character |
| 396 |
whose hexadecimal value is \fB0x\fIhh\fR. |
| 397 |
.TP |
| 398 |
\fB\e0\fR |
| 399 |
. |
| 400 |
the character whose value is \fB0\fR |
| 401 |
.TP |
| 402 |
\fB\e\fIxyz\fR |
| 403 |
. |
| 404 |
(where \fIxyz\fR is exactly three octal digits, and is not a \fIback |
| 405 |
reference\fR (see below)) the character whose octal value is |
| 406 |
\fB0\fIxyz\fR. The first digit must be in the range 0-3, otherwise |
| 407 |
the two-digit form is assumed. |
| 408 |
.TP |
| 409 |
\fB\e\fIxy\fR |
| 410 |
. |
| 411 |
(where \fIxy\fR is exactly two octal digits, and is not a \fIback |
| 412 |
reference\fR (see below)) the character whose octal value is |
| 413 |
\fB0\fIxy\fR |
| 414 |
.RE |
| 415 |
.PP |
| 416 |
Hexadecimal digits are |
| 417 |
.QR \fB0\fR \fB9\fR , |
| 418 |
.QR \fBa\fR \fBf\fR , |
| 419 |
and |
| 420 |
.QR \fBA\fR \fBF\fR . |
| 421 |
Octal digits are |
| 422 |
.QR \fB0\fR \fB7\fR . |
| 423 |
.PP |
| 424 |
The character-entry escapes are always taken as ordinary characters. |
| 425 |
For example, \fB\e135\fR is \fB]\fR in Unicode, but \fB\e135\fR does |
| 426 |
not terminate a bracket expression. Beware, however, that some |
| 427 |
applications (e.g., C compilers and the Tcl interpreter if the regular |
| 428 |
expression is not quoted with braces) interpret such sequences |
| 429 |
themselves before the regular-expression package gets to see them, |
| 430 |
which may require doubling (quadrupling, etc.) the |
| 431 |
.QW \fB\e\fR . |
| 432 |
.SS "CLASS-SHORTHAND ESCAPES" |
| 433 |
Class-shorthand escapes (AREs only) provide shorthands for certain |
| 434 |
commonly-used character classes: |
| 435 |
.RS 2 |
| 436 |
.TP 10 |
| 437 |
\fB\ed\fR |
| 438 |
. |
| 439 |
\fB[[:digit:]]\fR |
| 440 |
.TP |
| 441 |
\fB\es\fR |
| 442 |
. |
| 443 |
\fB[[:space:]]\fR |
| 444 |
.TP |
| 445 |
\fB\ew\fR |
| 446 |
. |
| 447 |
\fB[[:alnum:]_\eu203F\eu2040\eu2054\euFE33\euFE34\euFE4D\euFE4E\euFE4F\euFF3F]\fR (including punctuation connector characters) |
| 448 |
.TP |
| 449 |
\fB\eD\fR |
| 450 |
. |
| 451 |
\fB[^[:digit:]]\fR |
| 452 |
.TP |
| 453 |
\fB\eS\fR |
| 454 |
. |
| 455 |
\fB[^[:space:]]\fR |
| 456 |
.TP |
| 457 |
\fB\eW\fR |
| 458 |
. |
| 459 |
\fB[^[:alnum:]_\eu203F\eu2040\eu2054\euFE33\euFE34\euFE4D\euFE4E\euFE4F\euFF3F]\fR (including punctuation connector characters) |
| 460 |
.RE |
| 461 |
.PP |
| 462 |
Within bracket expressions, |
| 463 |
.QW \fB\ed\fR , |
| 464 |
.QW \fB\es\fR , |
| 465 |
and |
| 466 |
.QW \fB\ew\fR \& |
| 467 |
lose their outer brackets, and |
| 468 |
.QW \fB\eD\fR , |
| 469 |
.QW \fB\eS\fR , |
| 470 |
and |
| 471 |
.QW \fB\eW\fR \& |
| 472 |
are illegal. (So, for example, |
| 473 |
.QW \fB[a-c\ed]\fR |
| 474 |
is equivalent to |
| 475 |
.QW \fB[a-c[:digit:]]\fR . |
| 476 |
Also, |
| 477 |
.QW \fB[a-c\eD]\fR , |
| 478 |
which is equivalent to |
| 479 |
.QW \fB[a-c^[:digit:]]\fR , |
| 480 |
is illegal.) |
| 481 |
.SS "CONSTRAINT ESCAPES" |
| 482 |
A constraint escape (AREs only) is a constraint, matching the empty |
| 483 |
string if specific conditions are met, written as an escape: |
| 484 |
.RS 2 |
| 485 |
.TP 6 |
| 486 |
\fB\eA\fR |
| 487 |
. |
| 488 |
matches only at the beginning of the string (see \fBMATCHING\fR, |
| 489 |
below, for how this differs from |
| 490 |
.QW \fB^\fR ) |
| 491 |
.TP |
| 492 |
\fB\em\fR |
| 493 |
. |
| 494 |
matches only at the beginning of a word |
| 495 |
.TP |
| 496 |
\fB\eM\fR |
| 497 |
. |
| 498 |
matches only at the end of a word |
| 499 |
.TP |
| 500 |
\fB\ey\fR |
| 501 |
. |
| 502 |
matches only at the beginning or end of a word |
| 503 |
.TP |
| 504 |
\fB\eY\fR |
| 505 |
. |
| 506 |
matches only at a point that is not the beginning or end of a word |
| 507 |
.TP |
| 508 |
\fB\eZ\fR |
| 509 |
. |
| 510 |
matches only at the end of the string (see \fBMATCHING\fR, below, for |
| 511 |
how this differs from |
| 512 |
.QW \fB$\fR ) |
| 513 |
.TP |
| 514 |
\fB\e\fIm\fR |
| 515 |
. |
| 516 |
(where \fIm\fR is a nonzero digit) a \fIback reference\fR, see below |
| 517 |
.TP |
| 518 |
\fB\e\fImnn\fR |
| 519 |
. |
| 520 |
(where \fIm\fR is a nonzero digit, and \fInn\fR is some more digits, |
| 521 |
and the decimal value \fImnn\fR is not greater than the number of |
| 522 |
closing capturing parentheses seen so far) a \fIback reference\fR, see |
| 523 |
below |
| 524 |
.RE |
| 525 |
.PP |
| 526 |
A word is defined as in the specification of |
| 527 |
.QW \fB[[:<:]]\fR |
| 528 |
and |
| 529 |
.QW \fB[[:>:]]\fR |
| 530 |
above. Constraint escapes are illegal within bracket expressions. |
| 531 |
.SS "BACK REFERENCES" |
| 532 |
A back reference (AREs only) matches the same string matched by the |
| 533 |
parenthesized subexpression specified by the number, so that (e.g.) |
| 534 |
.QW \fB([bc])\e1\fR |
| 535 |
matches |
| 536 |
.QW \fBbb\fR |
| 537 |
or |
| 538 |
.QW \fBcc\fR |
| 539 |
but not |
| 540 |
.QW \fBbc\fR . |
| 541 |
The subexpression must entirely precede the back reference in the RE. |
| 542 |
Subexpressions are numbered in the order of their leading parentheses. |
| 543 |
Non-capturing parentheses do not define subexpressions. |
| 544 |
.PP |
| 545 |
There is an inherent historical ambiguity between octal |
| 546 |
character-entry escapes and back references, which is resolved by |
| 547 |
heuristics, as hinted at above. A leading zero always indicates an |
| 548 |
octal escape. A single non-zero digit, not followed by another digit, |
| 549 |
is always taken as a back reference. A multi-digit sequence not |
| 550 |
starting with a zero is taken as a back reference if it comes after a |
| 551 |
suitable subexpression (i.e. the number is in the legal range for a |
| 552 |
back reference), and otherwise is taken as octal. |
| 553 |
.SH "METASYNTAX" |
| 554 |
In addition to the main syntax described above, there are some special |
| 555 |
forms and miscellaneous syntactic facilities available. |
| 556 |
.PP |
| 557 |
Normally the flavor of RE being used is specified by |
| 558 |
application-dependent means. However, this can be overridden by a |
| 559 |
\fIdirector\fR. If an RE of any flavor begins with |
| 560 |
.QW \fB***:\fR , |
| 561 |
the rest of the RE is an ARE. If an RE of any flavor begins with |
| 562 |
.QW \fB***=\fR , |
| 563 |
the rest of the RE is taken to be a literal string, with |
| 564 |
all characters considered ordinary characters. |
| 565 |
.PP |
| 566 |
An ARE may begin with \fIembedded options\fR: a sequence |
| 567 |
\fB(?\fIxyz\fB)\fR (where \fIxyz\fR is one or more alphabetic |
| 568 |
characters) specifies options affecting the rest of the RE. These |
| 569 |
supplement, and can override, any options specified by the |
| 570 |
application. The available option letters are: |
| 571 |
.RS 2 |
| 572 |
.TP 3 |
| 573 |
\fBb\fR |
| 574 |
. |
| 575 |
rest of RE is a BRE |
| 576 |
.TP 3 |
| 577 |
\fBc\fR |
| 578 |
. |
| 579 |
case-sensitive matching (usual default) |
| 580 |
.TP 3 |
| 581 |
\fBe\fR |
| 582 |
. |
| 583 |
rest of RE is an ERE |
| 584 |
.TP 3 |
| 585 |
\fBi\fR |
| 586 |
. |
| 587 |
case-insensitive matching (see \fBMATCHING\fR, below) |
| 588 |
.TP 3 |
| 589 |
\fBm\fR |
| 590 |
. |
| 591 |
historical synonym for \fBn\fR |
| 592 |
.TP 3 |
| 593 |
\fBn\fR |
| 594 |
. |
| 595 |
newline-sensitive matching (see \fBMATCHING\fR, below) |
| 596 |
.TP 3 |
| 597 |
\fBp\fR |
| 598 |
. |
| 599 |
partial newline-sensitive matching (see \fBMATCHING\fR, below) |
| 600 |
.TP 3 |
| 601 |
\fBq\fR |
| 602 |
. |
| 603 |
rest of RE is a literal |
| 604 |
.PQ quoted |
| 605 |
string, all ordinary characters |
| 606 |
.TP 3 |
| 607 |
\fBs\fR |
| 608 |
. |
| 609 |
non-newline-sensitive matching (usual default) |
| 610 |
.TP 3 |
| 611 |
\fBt\fR |
| 612 |
. |
| 613 |
tight syntax (usual default; see below) |
| 614 |
.TP 3 |
| 615 |
\fBw\fR |
| 616 |
. |
| 617 |
inverse partial newline-sensitive |
| 618 |
.PQ weird |
| 619 |
matching (see \fBMATCHING\fR, below) |
| 620 |
.TP 3 |
| 621 |
\fBx\fR |
| 622 |
. |
| 623 |
expanded syntax (see below) |
| 624 |
.RE |
| 625 |
.PP |
| 626 |
Embedded options take effect at the \fB)\fR terminating the sequence. |
| 627 |
They are available only at the start of an ARE, and may not be used |
| 628 |
later within it. |
| 629 |
.PP |
| 630 |
In addition to the usual (\fItight\fR) RE syntax, in which all |
| 631 |
characters are significant, there is an \fIexpanded\fR syntax, |
| 632 |
available in all flavors of RE with the \fB\-expanded\fR switch, or in |
| 633 |
AREs with the embedded x option. In the expanded syntax, white-space |
| 634 |
characters are ignored and all characters between a \fB#\fR and the |
| 635 |
following newline (or the end of the RE) are ignored, permitting |
| 636 |
paragraphing and commenting a complex RE. There are three exceptions |
| 637 |
to that basic rule: |
| 638 |
.IP \(bu 3 |
| 639 |
a white-space character or |
| 640 |
.QW \fB#\fR |
| 641 |
preceded by |
| 642 |
.QW \fB\e\fR |
| 643 |
is retained |
| 644 |
.IP \(bu 3 |
| 645 |
white space or |
| 646 |
.QW \fB#\fR |
| 647 |
within a bracket expression is retained |
| 648 |
.IP \(bu 3 |
| 649 |
white space and comments are illegal within multi-character symbols |
| 650 |
like the ARE |
| 651 |
.QW \fB(?:\fR |
| 652 |
or the BRE |
| 653 |
.QW \fB\e(\fR |
| 654 |
.PP |
| 655 |
Expanded-syntax white-space characters are blank, tab, newline, and |
| 656 |
any character that belongs to the \fIspace\fR character class. |
| 657 |
.PP |
| 658 |
Finally, in an ARE, outside bracket expressions, the sequence |
| 659 |
.QW \fB(?#\fIttt\fB)\fR |
| 660 |
(where \fIttt\fR is any text not containing a |
| 661 |
.QW \fB)\fR ) |
| 662 |
is a comment, completely ignored. Again, this is not |
| 663 |
allowed between the characters of multi-character symbols like |
| 664 |
.QW \fB(?:\fR . |
| 665 |
Such comments are more a historical artifact than a useful facility, |
| 666 |
and their use is deprecated; use the expanded syntax instead. |
| 667 |
.PP |
| 668 |
\fINone\fR of these metasyntax extensions is available if the |
| 669 |
application (or an initial |
| 670 |
.QW \fB***=\fR |
| 671 |
director) has specified that the |
| 672 |
user's input be treated as a literal string rather than as an RE. |
| 673 |
.SH MATCHING |
| 674 |
In the event that an RE could match more than one substring of a given |
| 675 |
string, the RE matches the one starting earliest in the string. If |
| 676 |
the RE could match more than one substring starting at that point, its |
| 677 |
choice is determined by its \fIpreference\fR: either the longest |
| 678 |
substring, or the shortest. |
| 679 |
.PP |
| 680 |
Most atoms, and all constraints, have no preference. A parenthesized |
| 681 |
RE has the same preference (possibly none) as the RE. A quantified |
| 682 |
atom with quantifier \fB{\fIm\fB}\fR or \fB{\fIm\fB}?\fR has the same |
| 683 |
preference (possibly none) as the atom itself. A quantified atom with |
| 684 |
other normal quantifiers (including \fB{\fIm\fB,\fIn\fB}\fR with |
| 685 |
\fIm\fR equal to \fIn\fR) prefers longest match. A quantified atom |
| 686 |
with other non-greedy quantifiers (including \fB{\fIm\fB,\fIn\fB}?\fR |
| 687 |
with \fIm\fR equal to \fIn\fR) prefers shortest match. A branch has |
| 688 |
the same preference as the first quantified atom in it which has a |
| 689 |
preference. An RE consisting of two or more branches connected by the |
| 690 |
\fB|\fR operator prefers longest match. |
| 691 |
.PP |
| 692 |
Subject to the constraints imposed by the rules for matching the whole |
| 693 |
RE, subexpressions also match the longest or shortest possible |
| 694 |
substrings, based on their preferences, with subexpressions starting |
| 695 |
earlier in the RE taking priority over ones starting later. Note that |
| 696 |
outer subexpressions thus take priority over their component |
| 697 |
subexpressions. |
| 698 |
.PP |
| 699 |
The quantifiers \fB{1,1}\fR and \fB{1,1}?\fR can be used to |
| 700 |
force longest and shortest preference, respectively, on a |
| 701 |
subexpression or a whole RE. |
| 702 |
.RS |
| 703 |
.PP |
| 704 |
\fBNOTE:\fR This means that you can usually make a RE be non-greedy overall by |
| 705 |
putting \fB{1,1}?\fR after one of the first non-constraint atoms or |
| 706 |
parenthesized sub-expressions in it. \fIIt pays to experiment\fR with the |
| 707 |
placing of this non-greediness override on a suitable range of input texts |
| 708 |
when you are writing a RE if you are using this level of complexity. |
| 709 |
.PP |
| 710 |
For example, this regular expression is non-greedy, and will match the |
| 711 |
shortest substring possible given that |
| 712 |
.QW \fBabc\fR |
| 713 |
will be matched as early as possible (the quantifier does not change that): |
| 714 |
.PP |
| 715 |
.CS |
| 716 |
ab{1,1}?c.*x.*cba |
| 717 |
.CE |
| 718 |
.PP |
| 719 |
The atom |
| 720 |
.QW \fBa\fR |
| 721 |
has no greediness preference, we explicitly give one for |
| 722 |
.QW \fBb\fR , |
| 723 |
and the remaining quantifiers are overridden to be non-greedy by the preceding |
| 724 |
non-greedy quantifier. |
| 725 |
.RE |
| 726 |
.PP |
| 727 |
Match lengths are measured in characters, not collating elements. An |
| 728 |
empty string is considered longer than no match at all. For example, |
| 729 |
.QW \fBbb*\fR |
| 730 |
matches the three middle characters of |
| 731 |
.QW \fBabbbc\fR , |
| 732 |
.QW \fB(week|wee)(night|knights)\fR |
| 733 |
matches all ten characters of |
| 734 |
.QW \fBweeknights\fR , |
| 735 |
when |
| 736 |
.QW \fB(.*).*\fR |
| 737 |
is matched against |
| 738 |
.QW \fBabc\fR |
| 739 |
the parenthesized subexpression matches all three characters, and when |
| 740 |
.QW \fB(a*)*\fR |
| 741 |
is matched against |
| 742 |
.QW \fBbc\fR |
| 743 |
both the whole RE and the parenthesized subexpression match an empty string. |
| 744 |
.PP |
| 745 |
If case-independent matching is specified, the effect is much as if |
| 746 |
all case distinctions had vanished from the alphabet. When an |
| 747 |
alphabetic that exists in multiple cases appears as an ordinary |
| 748 |
character outside a bracket expression, it is effectively transformed |
| 749 |
into a bracket expression containing both cases, so that \fBx\fR |
| 750 |
becomes |
| 751 |
.QW \fB[xX]\fR . |
| 752 |
When it appears inside a bracket expression, |
| 753 |
all case counterparts of it are added to the bracket expression, so |
| 754 |
that |
| 755 |
.QW \fB[x]\fR |
| 756 |
becomes |
| 757 |
.QW \fB[xX]\fR |
| 758 |
and |
| 759 |
.QW \fB[^x]\fR |
| 760 |
becomes |
| 761 |
.QW \fB[^xX]\fR . |
| 762 |
.PP |
| 763 |
If newline-sensitive matching is specified, \fB.\fR and bracket |
| 764 |
expressions using \fB^\fR will never match the newline character (so |
| 765 |
that matches will never cross newlines unless the RE explicitly |
| 766 |
arranges it) and \fB^\fR and \fB$\fR will match the empty string after |
| 767 |
and before a newline respectively, in addition to matching at |
| 768 |
beginning and end of string respectively. ARE \fB\eA\fR and \fB\eZ\fR |
| 769 |
continue to match beginning or end of string \fIonly\fR. |
| 770 |
.PP |
| 771 |
If partial newline-sensitive matching is specified, this affects |
| 772 |
\fB.\fR and bracket expressions as with newline-sensitive matching, |
| 773 |
but not \fB^\fR and \fB$\fR. |
| 774 |
.PP |
| 775 |
If inverse partial newline-sensitive matching is specified, this |
| 776 |
affects \fB^\fR and \fB$\fR as with newline-sensitive matching, but |
| 777 |
not \fB.\fR and bracket expressions. This is not very useful but is |
| 778 |
provided for symmetry. |
| 779 |
.SH "LIMITS AND COMPATIBILITY" |
| 780 |
No particular limit is imposed on the length of REs. Programs |
| 781 |
intended to be highly portable should not employ REs longer than 256 |
| 782 |
bytes, as a POSIX-compliant implementation can refuse to accept such |
| 783 |
REs. |
| 784 |
.PP |
| 785 |
The only feature of AREs that is actually incompatible with POSIX EREs |
| 786 |
is that \fB\e\fR does not lose its special significance inside bracket |
| 787 |
expressions. All other ARE features use syntax which is illegal or |
| 788 |
has undefined or unspecified effects in POSIX EREs; the \fB***\fR |
| 789 |
syntax of directors likewise is outside the POSIX syntax for both BREs |
| 790 |
and EREs. |
| 791 |
.PP |
| 792 |
Many of the ARE extensions are borrowed from Perl, but some have been |
| 793 |
changed to clean them up, and a few Perl extensions are not present. |
| 794 |
Incompatibilities of note include |
| 795 |
.QW \fB\eb\fR , |
| 796 |
.QW \fB\eB\fR , |
| 797 |
the lack of special treatment for a trailing newline, the addition of |
| 798 |
complemented bracket expressions to the things affected by |
| 799 |
newline-sensitive matching, the restrictions on parentheses and back |
| 800 |
references in lookahead constraints, and the longest/shortest-match |
| 801 |
(rather than first-match) matching semantics. |
| 802 |
.PP |
| 803 |
The matching rules for REs containing both normal and non-greedy |
| 804 |
quantifiers have changed since early beta-test versions of this |
| 805 |
package. (The new rules are much simpler and cleaner, but do not work |
| 806 |
as hard at guessing the user's real intentions.) |
| 807 |
.PP |
| 808 |
Henry Spencer's original 1986 \fIregexp\fR package, still in |
| 809 |
widespread use (e.g., in pre-8.1 releases of Tcl), implemented an |
| 810 |
early version of today's EREs. There are four incompatibilities |
| 811 |
between \fIregexp\fR's near-EREs |
| 812 |
.PQ RREs " for short" |
| 813 |
and AREs. In roughly increasing order of significance: |
| 814 |
.IP \(bu 3 |
| 815 |
In AREs, \fB\e\fR followed by an alphanumeric character is either an |
| 816 |
escape or an error, while in RREs, it was just another way of writing |
| 817 |
the alphanumeric. This should not be a problem because there was no |
| 818 |
reason to write such a sequence in RREs. |
| 819 |
.IP \(bu 3 |
| 820 |
\fB{\fR followed by a digit in an ARE is the beginning of a bound, |
| 821 |
while in RREs, \fB{\fR was always an ordinary character. Such |
| 822 |
sequences should be rare, and will often result in an error because |
| 823 |
following characters will not look like a valid bound. |
| 824 |
.IP \(bu 3 |
| 825 |
In AREs, \fB\e\fR remains a special character within |
| 826 |
.QW \fB[\|]\fR , |
| 827 |
so a literal \fB\e\fR within \fB[\|]\fR must be written |
| 828 |
.QW \fB\e\e\fR . |
| 829 |
\fB\e\e\fR also gives a literal \fB\e\fR within \fB[\|]\fR in RREs, |
| 830 |
but only truly paranoid programmers routinely doubled the backslash. |
| 831 |
.IP \(bu 3 |
| 832 |
AREs report the longest/shortest match for the RE, rather than the |
| 833 |
first found in a specified search order. This may affect some RREs |
| 834 |
which were written in the expectation that the first match would be |
| 835 |
reported. (The careful crafting of RREs to optimize the search order |
| 836 |
for fast matching is obsolete (AREs examine all possible matches in |
| 837 |
parallel, and their performance is largely insensitive to their |
| 838 |
complexity) but cases where the search order was exploited to |
| 839 |
deliberately find a match which was \fInot\fR the longest/shortest |
| 840 |
will need rewriting.) |
| 841 |
.SH "BASIC REGULAR EXPRESSIONS" |
| 842 |
BREs differ from EREs in several respects. |
| 843 |
.QW \fB|\fR , |
| 844 |
.QW \fB+\fR , |
| 845 |
and \fB?\fR are ordinary characters and there is no equivalent for their |
| 846 |
functionality. The delimiters for bounds are \fB\e{\fR and |
| 847 |
.QW \fB\e}\fR , |
| 848 |
with \fB{\fR and \fB}\fR by themselves ordinary characters. The |
| 849 |
parentheses for nested subexpressions are \fB\e(\fR and |
| 850 |
.QW \fB\e)\fR , |
| 851 |
with \fB(\fR and \fB)\fR by themselves ordinary |
| 852 |
characters. \fB^\fR is an ordinary character except at the beginning |
| 853 |
of the RE or the beginning of a parenthesized subexpression, \fB$\fR |
| 854 |
is an ordinary character except at the end of the RE or the end of a |
| 855 |
parenthesized subexpression, and \fB*\fR is an ordinary character if |
| 856 |
it appears at the beginning of the RE or the beginning of a |
| 857 |
parenthesized subexpression (after a possible leading |
| 858 |
.QW \fB^\fR ). |
| 859 |
Finally, single-digit back references are available, and \fB\e<\fR and |
| 860 |
\fB\e>\fR are synonyms for |
| 861 |
.QW \fB[[:<:]]\fR |
| 862 |
and |
| 863 |
.QW \fB[[:>:]]\fR |
| 864 |
respectively; no other escapes are available. |
| 865 |
.SH "SEE ALSO" |
| 866 |
RegExp(3), regexp(n), regsub(n), lsearch(n), switch(n), text(n) |
| 867 |
.SH KEYWORDS |
| 868 |
match, regular expression, string |
| 869 |
.\" Local Variables: |
| 870 |
.\" mode: nroff |
| 871 |
.\" End: |