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