4 A @dfn{regular expression} (or @dfn{regexp}, or @dfn{pattern}) is a text
5 string that describes some (mathematical) set of strings. A regexp
6 @var{r} @dfn{matches} a string @var{s} if @var{s} is in the set of
7 strings described by @var{r}.
9 Using the Regex library, you can:
14 see if a string matches a specified pattern as a whole, and
17 search within a string for a substring matching a specified pattern.
21 Some regular expressions match only one string, i.e., the set they
22 describe has only one member. For example, the regular expression
23 @samp{foo} matches the string @samp{foo} and no others. Other regular
24 expressions match more than one string, i.e., the set they describe has
25 more than one member. For example, the regular expression @samp{f*}
26 matches the set of strings made up of any number (including zero) of
27 @samp{f}s. As you can see, some characters in regular expressions match
28 themselves (such as @samp{f}) and some don't (such as @samp{*}); the
29 ones that don't match themselves instead let you specify patterns that
30 describe many different strings.
32 To either match or search for a regular expression with the Regex
33 library functions, you must first compile it with a Regex pattern
34 compiling function. A @dfn{compiled pattern} is a regular expression
35 converted to the internal format used by the library functions. Once
36 you've compiled a pattern, you can use it for matching or searching any
39 The Regex library consists of two source files: @file{regex.h} and
43 Regex provides three groups of functions with which you can operate on
44 regular expressions. One group---the @sc{gnu} group---is more powerful
45 but not completely compatible with the other two, namely the @sc{posix}
46 and Berkeley @sc{unix} groups; its interface was designed specifically
47 for @sc{gnu}. The other groups have the same interfaces as do the
48 regular expression functions in @sc{posix} and Berkeley
51 We wrote this chapter with programmers in mind, not users of
52 programs---such as Emacs---that use Regex. We describe the Regex
53 library in its entirety, not how to write regular expressions that a
54 particular program understands.
57 @node Regular Expression Syntax
58 @chapter Regular Expression Syntax
60 @cindex regular expressions, syntax of
61 @cindex syntax of regular expressions
63 @dfn{Characters} are things you can type. @dfn{Operators} are things in
64 a regular expression that match one or more characters. You compose
65 regular expressions from operators, which in turn you specify using one
68 Most characters represent what we call the match-self operator, i.e.,
69 they match themselves; we call these characters @dfn{ordinary}. Other
70 characters represent either all or parts of fancier operators; e.g.,
71 @samp{.} represents what we call the match-any-character operator
72 (which, no surprise, matches (almost) any character); we call these
73 characters @dfn{special}. Two different things determine what
74 characters represent what operators:
78 the regular expression syntax your program has told the Regex library to
82 the context of the character in the regular expression.
85 In the following sections, we describe these things in more detail.
89 * Predefined Syntaxes::
90 * Collating Elements vs. Characters::
91 * The Backslash Character::
100 In any particular syntax for regular expressions, some characters are
101 always special, others are sometimes special, and others are never
102 special. The particular syntax that Regex recognizes for a given
103 regular expression depends on the value in the @code{syntax} field of
104 the pattern buffer of that regular expression.
106 You get a pattern buffer by compiling a regular expression. @xref{GNU
107 Pattern Buffers}, and @ref{POSIX Pattern Buffers}, for more information
108 on pattern buffers. @xref{GNU Regular Expression Compiling}, @ref{POSIX
109 Regular Expression Compiling}, and @ref{BSD Regular Expression
110 Compiling}, for more information on compiling.
112 Regex considers the value of the @code{syntax} field to be a collection
113 of bits; we refer to these bits as @dfn{syntax bits}. In most cases,
114 they affect what characters represent what operators. We describe the
115 meanings of the operators to which we refer in @ref{Common Operators},
116 @ref{GNU Operators}, and @ref{GNU Emacs Operators}.
118 For reference, here is the complete list of syntax bits, in alphabetical
123 @cnindex RE_BACKSLASH_ESCAPE_IN_LIST
124 @item RE_BACKSLASH_ESCAPE_IN_LISTS
125 If this bit is set, then @samp{\} inside a list (@pxref{List Operators}
126 quotes (makes ordinary, if it's special) the following character; if
127 this bit isn't set, then @samp{\} is an ordinary character inside lists.
128 (@xref{The Backslash Character}, for what `\' does outside of lists.)
130 @cnindex RE_BK_PLUS_QM
132 If this bit is set, then @samp{\+} represents the match-one-or-more
133 operator and @samp{\?} represents the match-zero-or-more operator; if
134 this bit isn't set, then @samp{+} represents the match-one-or-more
135 operator and @samp{?} represents the match-zero-or-one operator. This
136 bit is irrelevant if @code{RE_LIMITED_OPS} is set.
138 @cnindex RE_CHAR_CLASSES
139 @item RE_CHAR_CLASSES
140 If this bit is set, then you can use character classes in lists; if this
141 bit isn't set, then you can't.
143 @cnindex RE_CONTEXT_INDEP_ANCHORS
144 @item RE_CONTEXT_INDEP_ANCHORS
145 If this bit is set, then @samp{^} and @samp{$} are special anywhere outside
146 a list; if this bit isn't set, then these characters are special only in
147 certain contexts. @xref{Match-beginning-of-line Operator}, and
148 @ref{Match-end-of-line Operator}.
150 @cnindex RE_CONTEXT_INDEP_OPS
151 @item RE_CONTEXT_INDEP_OPS
152 If this bit is set, then certain characters are special anywhere outside
153 a list; if this bit isn't set, then those characters are special only in
154 some contexts and are ordinary elsewhere. Specifically, if this bit
155 isn't set then @samp{*}, and (if the syntax bit @code{RE_LIMITED_OPS}
156 isn't set) @samp{+} and @samp{?} (or @samp{\+} and @samp{\?}, depending
157 on the syntax bit @code{RE_BK_PLUS_QM}) represent repetition operators
158 only if they're not first in a regular expression or just after an
159 open-group or alternation operator. The same holds for @samp{@{} (or
160 @samp{\@{}, depending on the syntax bit @code{RE_NO_BK_BRACES}) if
161 it is the beginning of a valid interval and the syntax bit
162 @code{RE_INTERVALS} is set.
164 @cnindex RE_CONTEXT_INVALID_OPS
165 @item RE_CONTEXT_INVALID_OPS
166 If this bit is set, then repetition and alternation operators can't be
167 in certain positions within a regular expression. Specifically, the
168 regular expression is invalid if it has:
173 a repetition operator first in the regular expression or just after a
174 match-beginning-of-line, open-group, or alternation operator; or
177 an alternation operator first or last in the regular expression, just
178 before a match-end-of-line operator, or just after an alternation or
183 If this bit isn't set, then you can put the characters representing the
184 repetition and alternation characters anywhere in a regular expression.
185 Whether or not they will in fact be operators in certain positions
186 depends on other syntax bits.
188 @cnindex RE_DOT_NEWLINE
190 If this bit is set, then the match-any-character operator matches
191 a newline; if this bit isn't set, then it doesn't.
193 @cnindex RE_DOT_NOT_NULL
194 @item RE_DOT_NOT_NULL
195 If this bit is set, then the match-any-character operator doesn't match
196 a null character; if this bit isn't set, then it does.
198 @cnindex RE_INTERVALS
200 If this bit is set, then Regex recognizes interval operators; if this bit
201 isn't set, then it doesn't.
203 @cnindex RE_LIMITED_OPS
205 If this bit is set, then Regex doesn't recognize the match-one-or-more,
206 match-zero-or-one or alternation operators; if this bit isn't set, then
209 @cnindex RE_NEWLINE_ALT
211 If this bit is set, then newline represents the alternation operator; if
212 this bit isn't set, then newline is ordinary.
214 @cnindex RE_NO_BK_BRACES
215 @item RE_NO_BK_BRACES
216 If this bit is set, then @samp{@{} represents the open-interval operator
217 and @samp{@}} represents the close-interval operator; if this bit isn't
218 set, then @samp{\@{} represents the open-interval operator and
219 @samp{\@}} represents the close-interval operator. This bit is relevant
220 only if @code{RE_INTERVALS} is set.
222 @cnindex RE_NO_BK_PARENS
223 @item RE_NO_BK_PARENS
224 If this bit is set, then @samp{(} represents the open-group operator and
225 @samp{)} represents the close-group operator; if this bit isn't set, then
226 @samp{\(} represents the open-group operator and @samp{\)} represents
227 the close-group operator.
229 @cnindex RE_NO_BK_REFS
231 If this bit is set, then Regex doesn't recognize @samp{\}@var{digit} as
232 the back reference operator; if this bit isn't set, then it does.
234 @cnindex RE_NO_BK_VBAR
236 If this bit is set, then @samp{|} represents the alternation operator;
237 if this bit isn't set, then @samp{\|} represents the alternation
238 operator. This bit is irrelevant if @code{RE_LIMITED_OPS} is set.
240 @cnindex RE_NO_EMPTY_RANGES
241 @item RE_NO_EMPTY_RANGES
242 If this bit is set, then a regular expression with a range whose ending
243 point collates lower than its starting point is invalid; if this bit
244 isn't set, then Regex considers such a range to be empty.
246 @cnindex RE_UNMATCHED_RIGHT_PAREN_ORD
247 @item RE_UNMATCHED_RIGHT_PAREN_ORD
248 If this bit is set and the regular expression has no matching open-group
249 operator, then Regex considers what would otherwise be a close-group
250 operator (based on how @code{RE_NO_BK_PARENS} is set) to match @samp{)}.
255 @node Predefined Syntaxes
256 @section Predefined Syntaxes
258 If you're programming with Regex, you can set a pattern buffer's
259 (@pxref{GNU Pattern Buffers}, and @ref{POSIX Pattern Buffers})
260 @code{syntax} field either to an arbitrary combination of syntax bits
261 (@pxref{Syntax Bits}) or else to the configurations defined by Regex.
262 These configurations define the syntaxes used by certain
263 programs---@sc{gnu} Emacs,
272 Egrep---in addition to syntaxes for @sc{posix} basic and extended
275 The predefined syntaxes--taken directly from @file{regex.h}---are:
278 #define RE_SYNTAX_EMACS 0
280 #define RE_SYNTAX_AWK \
281 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
282 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
283 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
284 | RE_UNMATCHED_RIGHT_PAREN_ORD)
286 #define RE_SYNTAX_POSIX_AWK \
287 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
289 #define RE_SYNTAX_GREP \
290 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
291 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
294 #define RE_SYNTAX_EGREP \
295 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
296 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
297 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
300 #define RE_SYNTAX_POSIX_EGREP \
301 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
303 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
304 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
306 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
308 /* Syntax bits common to both basic and extended POSIX regex syntax. */
309 #define _RE_SYNTAX_POSIX_COMMON \
310 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
311 | RE_INTERVALS | RE_NO_EMPTY_RANGES)
313 #define RE_SYNTAX_POSIX_BASIC \
314 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
316 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
317 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
318 isn't minimal, since other operators, such as \`, aren't disabled. */
319 #define RE_SYNTAX_POSIX_MINIMAL_BASIC \
320 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
322 #define RE_SYNTAX_POSIX_EXTENDED \
323 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
324 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
325 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
326 | RE_UNMATCHED_RIGHT_PAREN_ORD)
328 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
329 replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
330 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
331 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
332 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
333 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
334 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
337 @node Collating Elements vs. Characters
338 @section Collating Elements vs.@: Characters
340 @sc{posix} generalizes the notion of a character to that of a
341 collating element. It defines a @dfn{collating element} to be ``a
342 sequence of one or more bytes defined in the current collating sequence
343 as a unit of collation.''
345 This generalizes the notion of a character in
346 two ways. First, a single character can map into two or more collating
347 elements. For example, the German
354 collates as the collating element @samp{s} followed by another collating
355 element @samp{s}. Second, two or more characters can map into one
356 collating element. For example, the Spanish @samp{ll} collates after
357 @samp{l} and before @samp{m}.
359 Since @sc{posix}'s ``collating element'' preserves the essential idea of
360 a ``character,'' we use the latter, more familiar, term in this document.
362 @node The Backslash Character
363 @section The Backslash Character
366 The @samp{\} character has one of four different meanings, depending on
367 the context in which you use it and what syntax bits are set
368 (@pxref{Syntax Bits}). It can: 1) stand for itself, 2) quote the next
369 character, 3) introduce an operator, or 4) do nothing.
373 It stands for itself inside a list
374 (@pxref{List Operators}) if the syntax bit
375 @code{RE_BACKSLASH_ESCAPE_IN_LISTS} is not set. For example, @samp{[\]}
376 would match @samp{\}.
379 It quotes (makes ordinary, if it's special) the next character when you
384 outside a list,@footnote{Sometimes
385 you don't have to explicitly quote special characters to make
386 them ordinary. For instance, most characters lose any special meaning
387 inside a list (@pxref{List Operators}). In addition, if the syntax bits
388 @code{RE_CONTEXT_INVALID_OPS} and @code{RE_CONTEXT_INDEP_OPS}
389 aren't set, then (for historical reasons) the matcher considers special
390 characters ordinary if they are in contexts where the operations they
391 represent make no sense; for example, then the match-zero-or-more
392 operator (represented by @samp{*}) matches itself in the regular
393 expression @samp{*foo} because there is no preceding expression on which
394 it can operate. It is poor practice, however, to depend on this
395 behavior; if you want a special character to be ordinary outside a list,
396 it's better to always quote it, regardless.} or
399 inside a list and the syntax bit @code{RE_BACKSLASH_ESCAPE_IN_LISTS} is set.
404 It introduces an operator when followed by certain ordinary
405 characters---sometimes only when certain syntax bits are set. See the
406 cases @code{RE_BK_PLUS_QM}, @code{RE_NO_BK_BRACES}, @code{RE_NO_BK_VAR},
407 @code{RE_NO_BK_PARENS}, @code{RE_NO_BK_REF} in @ref{Syntax Bits}. Also:
411 @samp{\b} represents the match-word-boundary operator
412 (@pxref{Match-word-boundary Operator}).
415 @samp{\B} represents the match-within-word operator
416 (@pxref{Match-within-word Operator}).
419 @samp{\<} represents the match-beginning-of-word operator @*
420 (@pxref{Match-beginning-of-word Operator}).
423 @samp{\>} represents the match-end-of-word operator
424 (@pxref{Match-end-of-word Operator}).
427 @samp{\w} represents the match-word-constituent operator
428 (@pxref{Match-word-constituent Operator}).
431 @samp{\W} represents the match-non-word-constituent operator
432 (@pxref{Match-non-word-constituent Operator}).
435 @samp{\`} represents the match-beginning-of-buffer
436 operator and @samp{\'} represents the match-end-of-buffer operator
437 (@pxref{Buffer Operators}).
440 If Regex was compiled with the C preprocessor symbol @code{emacs}
441 defined, then @samp{\s@var{class}} represents the match-syntactic-class
442 operator and @samp{\S@var{class}} represents the
443 match-not-syntactic-class operator (@pxref{Syntactic Class Operators}).
448 In all other cases, Regex ignores @samp{\}. For example,
449 @samp{\n} matches @samp{n}.
453 @node Common Operators
454 @chapter Common Operators
456 You compose regular expressions from operators. In the following
457 sections, we describe the regular expression operators specified by
458 @sc{posix}; @sc{gnu} also uses these. Most operators have more than one
459 representation as characters. @xref{Regular Expression Syntax}, for
460 what characters represent what operators under what circumstances.
462 For most operators that can be represented in two ways, one
463 representation is a single character and the other is that character
464 preceded by @samp{\}. For example, either @samp{(} or @samp{\(}
465 represents the open-group operator. Which one does depends on the
466 setting of a syntax bit, in this case @code{RE_NO_BK_PARENS}. Why is
467 this so? Historical reasons dictate some of the varying
468 representations, while @sc{posix} dictates others.
470 Finally, almost all characters lose any special meaning inside a list
471 (@pxref{List Operators}).
474 * Match-self Operator:: Ordinary characters.
475 * Match-any-character Operator:: .
476 * Concatenation Operator:: Juxtaposition.
477 * Repetition Operators:: * + ? @{@}
478 * Alternation Operator:: |
479 * List Operators:: [...] [^...]
480 * Grouping Operators:: (...)
481 * Back-reference Operator:: \digit
482 * Anchoring Operators:: ^ $
485 @node Match-self Operator
486 @section The Match-self Operator (@var{ordinary character})
488 This operator matches the character itself. All ordinary characters
489 (@pxref{Regular Expression Syntax}) represent this operator. For
490 example, @samp{f} is always an ordinary character, so the regular
491 expression @samp{f} matches only the string @samp{f}. In
492 particular, it does @emph{not} match the string @samp{ff}.
494 @node Match-any-character Operator
495 @section The Match-any-character Operator (@code{.})
499 This operator matches any single printing or nonprinting character
500 except it won't match a:
504 if the syntax bit @code{RE_DOT_NEWLINE} isn't set.
507 if the syntax bit @code{RE_DOT_NOT_NULL} is set.
511 The @samp{.} (period) character represents this operator. For example,
512 @samp{a.b} matches any three-character string beginning with @samp{a}
513 and ending with @samp{b}.
515 @node Concatenation Operator
516 @section The Concatenation Operator
518 This operator concatenates two regular expressions @var{a} and @var{b}.
519 No character represents this operator; you simply put @var{b} after
520 @var{a}. The result is a regular expression that will match a string if
521 @var{a} matches its first part and @var{b} matches the rest. For
522 example, @samp{xy} (two match-self operators) matches @samp{xy}.
524 @node Repetition Operators
525 @section Repetition Operators
527 Repetition operators repeat the preceding regular expression a specified
531 * Match-zero-or-more Operator:: *
532 * Match-one-or-more Operator:: +
533 * Match-zero-or-one Operator:: ?
534 * Interval Operators:: @{@}
537 @node Match-zero-or-more Operator
538 @subsection The Match-zero-or-more Operator (@code{*})
542 This operator repeats the smallest possible preceding regular expression
543 as many times as necessary (including zero) to match the pattern.
544 @samp{*} represents this operator. For example, @samp{o*}
545 matches any string made up of zero or more @samp{o}s. Since this
546 operator operates on the smallest preceding regular expression,
547 @samp{fo*} has a repeating @samp{o}, not a repeating @samp{fo}. So,
548 @samp{fo*} matches @samp{f}, @samp{fo}, @samp{foo}, and so on.
550 Since the match-zero-or-more operator is a suffix operator, it may be
551 useless as such when no regular expression precedes it. This is the
556 is first in a regular expression, or
559 follows a match-beginning-of-line, open-group, or alternation
565 Three different things can happen in these cases:
569 If the syntax bit @code{RE_CONTEXT_INVALID_OPS} is set, then the
570 regular expression is invalid.
573 If @code{RE_CONTEXT_INVALID_OPS} isn't set, but
574 @code{RE_CONTEXT_INDEP_OPS} is, then @samp{*} represents the
575 match-zero-or-more operator (which then operates on the empty string).
578 Otherwise, @samp{*} is ordinary.
583 The matcher processes a match-zero-or-more operator by first matching as
584 many repetitions of the smallest preceding regular expression as it can.
585 Then it continues to match the rest of the pattern.
587 If it can't match the rest of the pattern, it backtracks (as many times
588 as necessary), each time discarding one of the matches until it can
589 either match the entire pattern or be certain that it cannot get a
590 match. For example, when matching @samp{ca*ar} against @samp{caaar},
591 the matcher first matches all three @samp{a}s of the string with the
592 @samp{a*} of the regular expression. However, it cannot then match the
593 final @samp{ar} of the regular expression against the final @samp{r} of
594 the string. So it backtracks, discarding the match of the last @samp{a}
595 in the string. It can then match the remaining @samp{ar}.
598 @node Match-one-or-more Operator
599 @subsection The Match-one-or-more Operator (@code{+} or @code{\+})
603 If the syntax bit @code{RE_LIMITED_OPS} is set, then Regex doesn't recognize
604 this operator. Otherwise, if the syntax bit @code{RE_BK_PLUS_QM} isn't
605 set, then @samp{+} represents this operator; if it is, then @samp{\+}
608 This operator is similar to the match-zero-or-more operator except that
609 it repeats the preceding regular expression at least once;
610 @pxref{Match-zero-or-more Operator}, for what it operates on, how some
611 syntax bits affect it, and how Regex backtracks to match it.
613 For example, supposing that @samp{+} represents the match-one-or-more
614 operator; then @samp{ca+r} matches, e.g., @samp{car} and
615 @samp{caaaar}, but not @samp{cr}.
617 @node Match-zero-or-one Operator
618 @subsection The Match-zero-or-one Operator (@code{?} or @code{\?})
621 If the syntax bit @code{RE_LIMITED_OPS} is set, then Regex doesn't
622 recognize this operator. Otherwise, if the syntax bit
623 @code{RE_BK_PLUS_QM} isn't set, then @samp{?} represents this operator;
624 if it is, then @samp{\?} does.
626 This operator is similar to the match-zero-or-more operator except that
627 it repeats the preceding regular expression once or not at all;
628 @pxref{Match-zero-or-more Operator}, to see what it operates on, how
629 some syntax bits affect it, and how Regex backtracks to match it.
631 For example, supposing that @samp{?} represents the match-zero-or-one
632 operator; then @samp{ca?r} matches both @samp{car} and @samp{cr}, but
635 @node Interval Operators
636 @subsection Interval Operators (@code{@{} @dots{} @code{@}} or @code{\@{} @dots{} @code{\@}})
638 @cindex interval expression
644 If the syntax bit @code{RE_INTERVALS} is set, then Regex recognizes
645 @dfn{interval expressions}. They repeat the smallest possible preceding
646 regular expression a specified number of times.
648 If the syntax bit @code{RE_NO_BK_BRACES} is set, @samp{@{} represents
649 the @dfn{open-interval operator} and @samp{@}} represents the
650 @dfn{close-interval operator} ; otherwise, @samp{\@{} and @samp{\@}} do.
652 Specifically, supposing that @samp{@{} and @samp{@}} represent the
653 open-interval and close-interval operators; then:
656 @item @{@var{count}@}
657 matches exactly @var{count} occurrences of the preceding regular
661 matches @var{min} or more occurrences of the preceding regular
664 @item @{@var{min, max}@}
665 matches at least @var{min} but no more than @var{max} occurrences of
666 the preceding regular expression.
670 The interval expression (but not necessarily the regular expression that
671 contains it) is invalid if:
675 @var{min} is greater than @var{max}, or
678 any of @var{count}, @var{min}, or @var{max} are outside the range
679 zero to @code{RE_DUP_MAX} (which symbol @file{regex.h}
684 If the interval expression is invalid and the syntax bit
685 @code{RE_NO_BK_BRACES} is set, then Regex considers all the
686 characters in the would-be interval to be ordinary. If that bit
687 isn't set, then the regular expression is invalid.
689 If the interval expression is valid but there is no preceding regular
690 expression on which to operate, then if the syntax bit
691 @code{RE_CONTEXT_INVALID_OPS} is set, the regular expression is invalid.
692 If that bit isn't set, then Regex considers all the characters---other
693 than backslashes, which it ignores---in the would-be interval to be
697 @node Alternation Operator
698 @section The Alternation Operator (@code{|} or @code{\|})
702 @cindex alternation operator
705 If the syntax bit @code{RE_LIMITED_OPS} is set, then Regex doesn't
706 recognize this operator. Otherwise, if the syntax bit
707 @code{RE_NO_BK_VBAR} is set, then @samp{|} represents this operator;
708 otherwise, @samp{\|} does.
710 Alternatives match one of a choice of regular expressions:
711 if you put the character(s) representing the alternation operator between
712 any two regular expressions @var{a} and @var{b}, the result matches
713 the union of the strings that @var{a} and @var{b} match. For
714 example, supposing that @samp{|} is the alternation operator, then
715 @samp{foo|bar|quux} would match any of @samp{foo}, @samp{bar} or
719 @c Nobody needs to disallow empty alternatives any more.
720 If the syntax bit @code{RE_NO_EMPTY_ALTS} is set, then if either of the regular
721 expressions @var{a} or @var{b} is empty, the
722 regular expression is invalid. More precisely, if this syntax bit is
723 set, then the alternation operator can't:
727 be first or last in a regular expression;
730 follow either another alternation operator or an open-group operator
731 (@pxref{Grouping Operators}); or
734 precede a close-group operator.
739 For example, supposing @samp{(} and @samp{)} represent the open and
740 close-group operators, then @samp{|foo}, @samp{foo|}, @samp{foo||bar},
741 @samp{foo(|bar)}, and @samp{(foo|)bar} would all be invalid.
744 The alternation operator operates on the @emph{largest} possible
745 surrounding regular expressions. (Put another way, it has the lowest
746 precedence of any regular expression operator.)
747 Thus, the only way you can
748 delimit its arguments is to use grouping. For example, if @samp{(} and
749 @samp{)} are the open and close-group operators, then @samp{fo(o|b)ar}
750 would match either @samp{fooar} or @samp{fobar}. (@samp{foo|bar} would
751 match @samp{foo} or @samp{bar}.)
754 The matcher usually tries all combinations of alternatives so as to
755 match the longest possible string. For example, when matching
756 @samp{(fooq|foo)*(qbarquux|bar)} against @samp{fooqbarquux}, it cannot
757 take, say, the first (``depth-first'') combination it could match, since
758 then it would be content to match just @samp{fooqbar}.
760 @comment xx something about leftmost-longest
764 @section List Operators (@code{[} @dots{} @code{]} and @code{[^} @dots{} @code{]})
766 @cindex matching list
773 @cindex nonmatching list
774 @cindex matching newline
775 @cindex bracket expression
777 @dfn{Lists}, also called @dfn{bracket expressions}, are a set of one or
778 more items. An @dfn{item} is a character,
780 (These get added when they get implemented.)
781 a collating symbol, an equivalence class expression,
783 a character class expression, or a range expression. The syntax bits
784 affect which kinds of items you can put in a list. We explain the last
785 two items in subsections below. Empty lists are invalid.
787 A @dfn{matching list} matches a single character represented by one of
788 the list items. You form a matching list by enclosing one or more items
789 within an @dfn{open-matching-list operator} (represented by @samp{[})
790 and a @dfn{close-list operator} (represented by @samp{]}).
792 For example, @samp{[ab]} matches either @samp{a} or @samp{b}.
793 @samp{[ad]*} matches the empty string and any string composed of just
794 @samp{a}s and @samp{d}s in any order. Regex considers invalid a regular
795 expression with a @samp{[} but no matching
798 @dfn{Nonmatching lists} are similar to matching lists except that they
799 match a single character @emph{not} represented by one of the list
800 items. You use an @dfn{open-nonmatching-list operator} (represented by
801 @samp{[^}@footnote{Regex therefore doesn't consider the @samp{^} to be
802 the first character in the list. If you put a @samp{^} character first
803 in (what you think is) a matching list, you'll turn it into a
804 nonmatching list.}) instead of an open-matching-list operator to start a
807 For example, @samp{[^ab]} matches any character except @samp{a} or
810 If the @code{posix_newline} field in the pattern buffer (@pxref{GNU
811 Pattern Buffers} is set, then nonmatching lists do not match a newline.
813 Most characters lose any special meaning inside a list. The special
814 characters inside a list follow.
818 ends the list if it's not the first list item. So, if you want to make
819 the @samp{]} character a list item, you must put it first.
822 quotes the next character if the syntax bit @code{RE_BACKSLASH_ESCAPE_IN_LISTS} is
826 Put these in if they get implemented.
829 represents the open-collating-symbol operator (@pxref{Collating Symbol
833 represents the close-collating-symbol operator.
836 represents the open-equivalence-class operator (@pxref{Equivalence Class
840 represents the close-equivalence-class operator.
845 represents the open-character-class operator (@pxref{Character Class
846 Operators}) if the syntax bit @code{RE_CHAR_CLASSES} is set and what
847 follows is a valid character class expression.
850 represents the close-character-class operator if the syntax bit
851 @code{RE_CHAR_CLASSES} is set and what precedes it is an
852 open-character-class operator followed by a valid character class name.
855 represents the range operator (@pxref{Range Operator}) if it's
856 not first or last in a list or the ending point of a range.
861 All other characters are ordinary. For example, @samp{[.*]} matches
862 @samp{.} and @samp{*}.
865 * Character Class Operators:: [:class:]
866 * Range Operator:: start-end
870 (If collating symbols and equivalence class expressions get implemented,
873 node Collating Symbol Operators
874 subsubsection Collating Symbol Operators (@code{[.} @dots{} @code{.]})
876 If the syntax bit @code{XX} is set, then you can represent
877 collating symbols inside lists. You form a @dfn{collating symbol} by
878 putting a collating element between an @dfn{open-collating-symbol
879 operator} and an @dfn{close-collating-symbol operator}. @samp{[.}
880 represents the open-collating-symbol operator and @samp{.]} represents
881 the close-collating-symbol operator. For example, if @samp{ll} is a
882 collating element, then @samp{[[.ll.]]} would match @samp{ll}.
884 node Equivalence Class Operators
885 subsubsection Equivalence Class Operators (@code{[=} @dots{} @code{=]})
886 @cindex equivalence class expression in regex
887 @cindex @samp{[=} in regex
888 @cindex @samp{=]} in regex
890 If the syntax bit @code{XX} is set, then Regex recognizes equivalence class
891 expressions inside lists. A @dfn{equivalence class expression} is a set
892 of collating elements which all belong to the same equivalence class.
893 You form an equivalence class expression by putting a collating
894 element between an @dfn{open-equivalence-class operator} and a
895 @dfn{close-equivalence-class operator}. @samp{[=} represents the
896 open-equivalence-class operator and @samp{=]} represents the
897 close-equivalence-class operator. For example, if @samp{a} and @samp{A}
898 were an equivalence class, then both @samp{[[=a=]]} and @samp{[[=A=]]}
899 would match both @samp{a} and @samp{A}. If the collating element in an
900 equivalence class expression isn't part of an equivalence class, then
901 the matcher considers the equivalence class expression to be a collating
906 @node Character Class Operators
907 @subsection Character Class Operators (@code{[:} @dots{} @code{:]})
909 @cindex character classes
910 @cindex @samp{[:} in regex
911 @cindex @samp{:]} in regex
913 If the syntax bit @code{RE_CHARACTER_CLASSES} is set, then Regex
914 recognizes character class expressions inside lists. A @dfn{character
915 class expression} matches one character from a given class. You form a
916 character class expression by putting a character class name between an
917 @dfn{open-character-class operator} (represented by @samp{[:}) and a
918 @dfn{close-character-class operator} (represented by @samp{:]}). The
919 character class names and their meanings are:
930 system-dependent; for @sc{gnu}, a space or tab
933 control characters (in the @sc{ascii} encoding, code 0177 and codes
940 same as @code{print} except omits space
946 printable characters (in the @sc{ascii} encoding, space
947 tilde---codes 040 through 0176)
950 neither control nor alphanumeric characters
953 space, carriage return, newline, vertical tab, and form feed
959 hexadecimal digits: @code{0}--@code{9}, @code{a}--@code{f}, @code{A}--@code{F}
964 These correspond to the definitions in the C library's @file{<ctype.h>}
965 facility. For example, @samp{[:alpha:]} corresponds to the standard
966 facility @code{isalpha}. Regex recognizes character class expressions
967 only inside of lists; so @samp{[[:alpha:]]} matches any letter, but
968 @samp{[:alpha:]} outside of a bracket expression and not followed by a
969 repetition operator matches just itself.
972 @subsection The Range Operator (@code{-})
974 Regex recognizes @dfn{range expressions} inside a list. They represent
976 that fall between two elements in the current collating sequence. You
977 form a range expression by putting a @dfn{range operator} between two
979 (If these get implemented, then substitute this for ``characters.'')
980 of any of the following: characters, collating elements, collating symbols,
981 and equivalence class expressions. The starting point of the range and
982 the ending point of the range don't have to be the same kind of item,
983 e.g., the starting point could be a collating element and the ending
984 point could be an equivalence class expression. If a range's ending
985 point is an equivalence class, then all the collating elements in that
986 class will be in the range.
988 characters.@footnote{You can't use a character class for the starting
989 or ending point of a range, since a character class is not a single
990 character.} @samp{-} represents the range operator. For example,
991 @samp{a-f} within a list represents all the characters from @samp{a}
995 If the syntax bit @code{RE_NO_EMPTY_RANGES} is set, then if the range's
996 ending point collates less than its starting point, the range (and the
997 regular expression containing it) is invalid. For example, the regular
998 expression @samp{[z-a]} would be invalid. If this bit isn't set, then
999 Regex considers such a range to be empty.
1001 Since @samp{-} represents the range operator, if you want to make a
1002 @samp{-} character itself
1003 a list item, you must do one of the following:
1007 Put the @samp{-} either first or last in the list.
1010 Include a range whose starting point collates strictly lower than
1011 @samp{-} and whose ending point collates equal or higher. Unless a
1012 range is the first item in a list, a @samp{-} can't be its starting
1013 point, but @emph{can} be its ending point. That is because Regex
1014 considers @samp{-} to be the range operator unless it is preceded by
1015 another @samp{-}. For example, in the @sc{ascii} encoding, @samp{)},
1016 @samp{*}, @samp{+}, @samp{,}, @samp{-}, @samp{.}, and @samp{/} are
1017 contiguous characters in the collating sequence. You might think that
1018 @samp{[)-+--/]} has two ranges: @samp{)-+} and @samp{--/}. Rather, it
1019 has the ranges @samp{)-+} and @samp{+--}, plus the character @samp{/}, so
1020 it matches, e.g., @samp{,}, not @samp{.}.
1023 Put a range whose starting point is @samp{-} first in the list.
1027 For example, @samp{[-a-z]} matches a lowercase letter or a hyphen (in
1028 English, in @sc{ascii}).
1031 @node Grouping Operators
1032 @section Grouping Operators (@code{(} @dots{} @code{)} or @code{\(} @dots{} @code{\)})
1039 @cindex subexpressions
1040 @cindex parenthesizing
1042 A @dfn{group}, also known as a @dfn{subexpression}, consists of an
1043 @dfn{open-group operator}, any number of other operators, and a
1044 @dfn{close-group operator}. Regex treats this sequence as a unit, just
1045 as mathematics and programming languages treat a parenthesized
1046 expression as a unit.
1048 Therefore, using @dfn{groups}, you can:
1052 delimit the argument(s) to an alternation operator (@pxref{Alternation
1053 Operator}) or a repetition operator (@pxref{Repetition
1057 keep track of the indices of the substring that matched a given group.
1058 @xref{Using Registers}, for a precise explanation.
1063 use the back-reference operator (@pxref{Back-reference Operator}).
1066 use registers (@pxref{Using Registers}).
1072 If the syntax bit @code{RE_NO_BK_PARENS} is set, then @samp{(} represents
1073 the open-group operator and @samp{)} represents the
1074 close-group operator; otherwise, @samp{\(} and @samp{\)} do.
1076 If the syntax bit @code{RE_UNMATCHED_RIGHT_PAREN_ORD} is set and a
1077 close-group operator has no matching open-group operator, then Regex
1078 considers it to match @samp{)}.
1081 @node Back-reference Operator
1082 @section The Back-reference Operator (@dfn{\}@var{digit})
1084 @cindex back references
1086 If the syntax bit @code{RE_NO_BK_REF} isn't set, then Regex recognizes
1087 back references. A back reference matches a specified preceding group.
1088 The back reference operator is represented by @samp{\@var{digit}}
1089 anywhere after the end of a regular expression's @w{@var{digit}-th}
1090 group (@pxref{Grouping Operators}).
1092 @var{digit} must be between @samp{1} and @samp{9}. The matcher assigns
1093 numbers 1 through 9 to the first nine groups it encounters. By using
1094 one of @samp{\1} through @samp{\9} after the corresponding group's
1095 close-group operator, you can match a substring identical to the
1096 one that the group does.
1098 Back references match according to the following (in all examples below,
1099 @samp{(} represents the open-group, @samp{)} the close-group, @samp{@{}
1100 the open-interval and @samp{@}} the close-interval operator):
1104 If the group matches a substring, the back reference matches an
1105 identical substring. For example, @samp{(a)\1} matches @samp{aa} and
1106 @samp{(bana)na\1bo\1} matches @samp{bananabanabobana}. Likewise,
1107 @samp{(.*)\1} matches any (newline-free if the syntax bit
1108 @code{RE_DOT_NEWLINE} isn't set) string that is composed of two
1109 identical halves; the @samp{(.*)} matches the first half and the
1110 @samp{\1} matches the second half.
1113 If the group matches more than once (as it might if followed
1114 by, e.g., a repetition operator), then the back reference matches the
1115 substring the group @emph{last} matched. For example,
1116 @samp{((a*)b)*\1\2} matches @samp{aabababa}; first @w{group 1} (the
1117 outer one) matches @samp{aab} and @w{group 2} (the inner one) matches
1118 @samp{aa}. Then @w{group 1} matches @samp{ab} and @w{group 2} matches
1119 @samp{a}. So, @samp{\1} matches @samp{ab} and @samp{\2} matches
1123 If the group doesn't participate in a match, i.e., it is part of an
1124 alternative not taken or a repetition operator allows zero repetitions
1125 of it, then the back reference makes the whole match fail. For example,
1126 @samp{(one()|two())-and-(three\2|four\3)} matches @samp{one-and-three}
1127 and @samp{two-and-four}, but not @samp{one-and-four} or
1128 @samp{two-and-three}. For example, if the pattern matches
1129 @samp{one-and-}, then its @w{group 2} matches the empty string and its
1130 @w{group 3} doesn't participate in the match. So, if it then matches
1131 @samp{four}, then when it tries to back reference @w{group 3}---which it
1132 will attempt to do because @samp{\3} follows the @samp{four}---the match
1133 will fail because @w{group 3} didn't participate in the match.
1137 You can use a back reference as an argument to a repetition operator. For
1138 example, @samp{(a(b))\2*} matches @samp{a} followed by two or more
1139 @samp{b}s. Similarly, @samp{(a(b))\2@{3@}} matches @samp{abbbb}.
1141 If there is no preceding @w{@var{digit}-th} subexpression, the regular
1142 expression is invalid.
1145 @node Anchoring Operators
1146 @section Anchoring Operators
1149 @cindex regexp anchoring
1151 These operators can constrain a pattern to match only at the beginning or
1152 end of the entire string or at the beginning or end of a line.
1155 * Match-beginning-of-line Operator:: ^
1156 * Match-end-of-line Operator:: $
1160 @node Match-beginning-of-line Operator
1161 @subsection The Match-beginning-of-line Operator (@code{^})
1164 @cindex beginning-of-line operator
1167 This operator can match the empty string either at the beginning of the
1168 string or after a newline character. Thus, it is said to @dfn{anchor}
1169 the pattern to the beginning of a line.
1171 In the cases following, @samp{^} represents this operator. (Otherwise,
1172 @samp{^} is ordinary.)
1177 It (the @samp{^}) is first in the pattern, as in @samp{^foo}.
1179 @cnindex RE_CONTEXT_INDEP_ANCHORS @r{(and @samp{^})}
1181 The syntax bit @code{RE_CONTEXT_INDEP_ANCHORS} is set, and it is outside
1182 a bracket expression.
1184 @cindex open-group operator and @samp{^}
1185 @cindex alternation operator and @samp{^}
1187 It follows an open-group or alternation operator, as in @samp{a\(^b\)}
1188 and @samp{a\|^b}. @xref{Grouping Operators}, and @ref{Alternation
1193 These rules imply that some valid patterns containing @samp{^} cannot be
1194 matched; for example, @samp{foo^bar} if @code{RE_CONTEXT_INDEP_ANCHORS}
1197 @vindex not_bol @r{field in pattern buffer}
1198 If the @code{not_bol} field is set in the pattern buffer (@pxref{GNU
1199 Pattern Buffers}), then @samp{^} fails to match at the beginning of the
1200 string. @xref{POSIX Matching}, for when you might find this useful.
1202 @vindex newline_anchor @r{field in pattern buffer}
1203 If the @code{newline_anchor} field is set in the pattern buffer, then
1204 @samp{^} fails to match after a newline. This is useful when you do not
1205 regard the string to be matched as broken into lines.
1208 @node Match-end-of-line Operator
1209 @subsection The Match-end-of-line Operator (@code{$})
1212 @cindex end-of-line operator
1215 This operator can match the empty string either at the end of
1216 the string or before a newline character in the string. Thus, it is
1217 said to @dfn{anchor} the pattern to the end of a line.
1219 It is always represented by @samp{$}. For example, @samp{foo$} usually
1220 matches, e.g., @samp{foo} and, e.g., the first three characters of
1223 Its interaction with the syntax bits and pattern buffer fields is
1224 exactly the dual of @samp{^}'s; see the previous section. (That is,
1225 ``beginning'' becomes ``end'', ``next'' becomes ``previous'', and
1226 ``after'' becomes ``before''.)
1230 @chapter GNU Operators
1232 Following are operators that @sc{gnu} defines (and @sc{posix} doesn't).
1236 * Buffer Operators::
1239 @node Word Operators
1240 @section Word Operators
1242 The operators in this section require Regex to recognize parts of words.
1243 Regex uses a syntax table to determine whether or not a character is
1244 part of a word, i.e., whether or not it is @dfn{word-constituent}.
1247 * Non-Emacs Syntax Tables::
1248 * Match-word-boundary Operator:: \b
1249 * Match-within-word Operator:: \B
1250 * Match-beginning-of-word Operator:: \<
1251 * Match-end-of-word Operator:: \>
1252 * Match-word-constituent Operator:: \w
1253 * Match-non-word-constituent Operator:: \W
1256 @node Non-Emacs Syntax Tables
1257 @subsection Non-Emacs Syntax Tables
1259 A @dfn{syntax table} is an array indexed by the characters in your
1260 character set. In the @sc{ascii} encoding, therefore, a syntax table
1261 has 256 elements. Regex always uses a @code{char *} variable
1262 @code{re_syntax_table} as its syntax table. In some cases, it
1263 initializes this variable and in others it expects you to initialize it.
1267 If Regex is compiled with the preprocessor symbols @code{emacs} and
1268 @code{SYNTAX_TABLE} both undefined, then Regex allocates
1269 @code{re_syntax_table} and initializes an element @var{i} either to
1270 @code{Sword} (which it defines) if @var{i} is a letter, number, or
1271 @samp{_}, or to zero if it's not.
1274 If Regex is compiled with @code{emacs} undefined but @code{SYNTAX_TABLE}
1275 defined, then Regex expects you to define a @code{char *} variable
1276 @code{re_syntax_table} to be a valid syntax table.
1279 @xref{Emacs Syntax Tables}, for what happens when Regex is compiled with
1280 the preprocessor symbol @code{emacs} defined.
1284 @node Match-word-boundary Operator
1285 @subsection The Match-word-boundary Operator (@code{\b})
1288 @cindex word boundaries, matching
1290 This operator (represented by @samp{\b}) matches the empty string at
1291 either the beginning or the end of a word. For example, @samp{\brat\b}
1292 matches the separate word @samp{rat}.
1294 @node Match-within-word Operator
1295 @subsection The Match-within-word Operator (@code{\B})
1299 This operator (represented by @samp{\B}) matches the empty string within
1300 a word. For example, @samp{c\Brat\Be} matches @samp{crate}, but
1301 @samp{dirty \Brat} doesn't match @samp{dirty rat}.
1303 @node Match-beginning-of-word Operator
1304 @subsection The Match-beginning-of-word Operator (@code{\<})
1308 This operator (represented by @samp{\<}) matches the empty string at the
1309 beginning of a word.
1311 @node Match-end-of-word Operator
1312 @subsection The Match-end-of-word Operator (@code{\>})
1316 This operator (represented by @samp{\>}) matches the empty string at the
1319 @node Match-word-constituent Operator
1320 @subsection The Match-word-constituent Operator (@code{\w})
1324 This operator (represented by @samp{\w}) matches any word-constituent
1327 @node Match-non-word-constituent Operator
1328 @subsection The Match-non-word-constituent Operator (@code{\W})
1332 This operator (represented by @samp{\W}) matches any character that is
1333 not word-constituent.
1336 @node Buffer Operators
1337 @section Buffer Operators
1339 Following are operators which work on buffers. In Emacs, a @dfn{buffer}
1340 is, naturally, an Emacs buffer. For other programs, Regex considers the
1341 entire string to be matched as the buffer.
1344 * Match-beginning-of-buffer Operator:: \`
1345 * Match-end-of-buffer Operator:: \'
1349 @node Match-beginning-of-buffer Operator
1350 @subsection The Match-beginning-of-buffer Operator (@code{\`})
1354 This operator (represented by @samp{\`}) matches the empty string at the
1355 beginning of the buffer.
1357 @node Match-end-of-buffer Operator
1358 @subsection The Match-end-of-buffer Operator (@code{\'})
1362 This operator (represented by @samp{\'}) matches the empty string at the
1366 @node GNU Emacs Operators
1367 @chapter GNU Emacs Operators
1369 Following are operators that @sc{gnu} defines (and @sc{posix} doesn't)
1370 that you can use only when Regex is compiled with the preprocessor
1371 symbol @code{emacs} defined.
1374 * Syntactic Class Operators::
1378 @node Syntactic Class Operators
1379 @section Syntactic Class Operators
1381 The operators in this section require Regex to recognize the syntactic
1382 classes of characters. Regex uses a syntax table to determine this.
1385 * Emacs Syntax Tables::
1386 * Match-syntactic-class Operator:: \sCLASS
1387 * Match-not-syntactic-class Operator:: \SCLASS
1390 @node Emacs Syntax Tables
1391 @subsection Emacs Syntax Tables
1393 A @dfn{syntax table} is an array indexed by the characters in your
1394 character set. In the @sc{ascii} encoding, therefore, a syntax table
1397 If Regex is compiled with the preprocessor symbol @code{emacs} defined,
1398 then Regex expects you to define and initialize the variable
1399 @code{re_syntax_table} to be an Emacs syntax table. Emacs' syntax
1400 tables are more complicated than Regex's own (@pxref{Non-Emacs Syntax
1401 Tables}). @xref{Syntax, , Syntax, emacs, The GNU Emacs User's Manual},
1402 for a description of Emacs' syntax tables.
1404 @node Match-syntactic-class Operator
1405 @subsection The Match-syntactic-class Operator (@code{\s}@var{class})
1409 This operator matches any character whose syntactic class is represented
1410 by a specified character. @samp{\s@var{class}} represents this operator
1411 where @var{class} is the character representing the syntactic class you
1412 want. For example, @samp{w} represents the syntactic
1413 class of word-constituent characters, so @samp{\sw} matches any
1414 word-constituent character.
1416 @node Match-not-syntactic-class Operator
1417 @subsection The Match-not-syntactic-class Operator (@code{\S}@var{class})
1421 This operator is similar to the match-syntactic-class operator except
1422 that it matches any character whose syntactic class is @emph{not}
1423 represented by the specified character. @samp{\S@var{class}} represents
1424 this operator. For example, @samp{w} represents the syntactic class of
1425 word-constituent characters, so @samp{\Sw} matches any character that is
1426 not word-constituent.
1429 @node What Gets Matched?
1430 @chapter What Gets Matched?
1432 Regex usually matches strings according to the ``leftmost longest''
1433 rule; that is, it chooses the longest of the leftmost matches. This
1434 does not mean that for a regular expression containing subexpressions
1435 that it simply chooses the longest match for each subexpression, left to
1436 right; the overall match must also be the longest possible one.
1438 For example, @samp{(ac*)(c*d[ac]*)\1} matches @samp{acdacaaa}, not
1439 @samp{acdac}, as it would if it were to choose the longest match for the
1440 first subexpression.
1443 @node Programming with Regex
1444 @chapter Programming with Regex
1446 Here we describe how you use the Regex data structures and functions in
1447 C programs. Regex has three interfaces: one designed for @sc{gnu}, one
1448 compatible with @sc{posix} and one compatible with Berkeley @sc{unix}.
1451 * GNU Regex Functions::
1452 * POSIX Regex Functions::
1453 * BSD Regex Functions::
1457 @node GNU Regex Functions
1458 @section GNU Regex Functions
1460 If you're writing code that doesn't need to be compatible with either
1461 @sc{posix} or Berkeley @sc{unix}, you can use these functions. They
1462 provide more options than the other interfaces.
1465 * GNU Pattern Buffers:: The re_pattern_buffer type.
1466 * GNU Regular Expression Compiling:: re_compile_pattern ()
1467 * GNU Matching:: re_match ()
1468 * GNU Searching:: re_search ()
1469 * Matching/Searching with Split Data:: re_match_2 (), re_search_2 ()
1470 * Searching with Fastmaps:: re_compile_fastmap ()
1471 * GNU Translate Tables:: The `translate' field.
1472 * Using Registers:: The re_registers type and related fns.
1473 * Freeing GNU Pattern Buffers:: regfree ()
1477 @node GNU Pattern Buffers
1478 @subsection GNU Pattern Buffers
1480 @cindex pattern buffer, definition of
1481 @tindex re_pattern_buffer @r{definition}
1482 @tindex struct re_pattern_buffer @r{definition}
1484 To compile, match, or search for a given regular expression, you must
1485 supply a pattern buffer. A @dfn{pattern buffer} holds one compiled
1486 regular expression.@footnote{Regular expressions are also referred to as
1487 ``patterns,'' hence the name ``pattern buffer.''}
1489 You can have several different pattern buffers simultaneously, each
1490 holding a compiled pattern for a different regular expression.
1492 @file{regex.h} defines the pattern buffer @code{struct} as follows:
1495 /* Space that holds the compiled pattern. It is declared as
1496 `unsigned char *' because its elements are
1497 sometimes used as array indexes. */
1498 unsigned char *buffer;
1500 /* Number of bytes to which `buffer' points. */
1501 unsigned long allocated;
1503 /* Number of bytes actually used in `buffer'. */
1506 /* Syntax setting with which the pattern was compiled. */
1507 reg_syntax_t syntax;
1509 /* Pointer to a fastmap, if any, otherwise zero. re_search uses
1510 the fastmap, if there is one, to skip over impossible
1511 starting points for matches. */
1514 /* Either a translate table to apply to all characters before
1515 comparing them, or zero for no translation. The translation
1516 is applied to a pattern when it is compiled and to a string
1517 when it is matched. */
1520 /* Number of subexpressions found by the compiler. */
1523 /* Zero if this pattern cannot match the empty string, one else.
1524 Well, in truth it's used only in `re_search_2', to see
1525 whether or not we should use the fastmap, so we don't set
1526 this absolutely perfectly; see `re_compile_fastmap' (the
1527 `duplicate' case). */
1528 unsigned can_be_null : 1;
1530 /* If REGS_UNALLOCATED, allocate space in the `regs' structure
1531 for `max (RE_NREGS, re_nsub + 1)' groups.
1532 If REGS_REALLOCATE, reallocate space if necessary.
1533 If REGS_FIXED, use what's there. */
1534 #define REGS_UNALLOCATED 0
1535 #define REGS_REALLOCATE 1
1536 #define REGS_FIXED 2
1537 unsigned regs_allocated : 2;
1539 /* Set to zero when `regex_compile' compiles a pattern; set to one
1540 by `re_compile_fastmap' if it updates the fastmap. */
1541 unsigned fastmap_accurate : 1;
1543 /* If set, `re_match_2' does not return information about
1545 unsigned no_sub : 1;
1547 /* If set, a beginning-of-line anchor doesn't match at the
1548 beginning of the string. */
1549 unsigned not_bol : 1;
1551 /* Similarly for an end-of-line anchor. */
1552 unsigned not_eol : 1;
1554 /* If true, an anchor at a newline matches. */
1555 unsigned newline_anchor : 1;
1560 @node GNU Regular Expression Compiling
1561 @subsection GNU Regular Expression Compiling
1563 In @sc{gnu}, you can both match and search for a given regular
1564 expression. To do either, you must first compile it in a pattern buffer
1565 (@pxref{GNU Pattern Buffers}).
1567 @cindex syntax initialization
1568 @vindex re_syntax_options @r{initialization}
1569 Regular expressions match according to the syntax with which they were
1570 compiled; with @sc{gnu}, you indicate what syntax you want by setting
1571 the variable @code{re_syntax_options} (declared in @file{regex.h} and
1572 defined in @file{regex.c}) before calling the compiling function,
1573 @code{re_compile_pattern} (see below). @xref{Syntax Bits}, and
1574 @ref{Predefined Syntaxes}.
1576 You can change the value of @code{re_syntax_options} at any time.
1577 Usually, however, you set its value once and then never change it.
1579 @cindex pattern buffer initialization
1580 @code{re_compile_pattern} takes a pattern buffer as an argument. You
1581 must initialize the following fields:
1585 @item translate @r{initialization}
1588 @vindex translate @r{initialization}
1589 Initialize this to point to a translate table if you want one, or to
1590 zero if you don't. We explain translate tables in @ref{GNU Translate
1594 @vindex fastmap @r{initialization}
1595 Initialize this to nonzero if you want a fastmap, or to zero if you
1600 @vindex buffer @r{initialization}
1601 @vindex allocated @r{initialization}
1603 If you want @code{re_compile_pattern} to allocate memory for the
1604 compiled pattern, set both of these to zero. If you have an existing
1605 block of memory (allocated with @code{malloc}) you want Regex to use,
1606 set @code{buffer} to its address and @code{allocated} to its size (in
1609 @code{re_compile_pattern} uses @code{realloc} to extend the space for
1610 the compiled pattern as necessary.
1614 To compile a pattern buffer, use:
1616 @findex re_compile_pattern
1619 re_compile_pattern (const char *@var{regex}, const int @var{regex_size},
1620 struct re_pattern_buffer *@var{pattern_buffer})
1624 @var{regex} is the regular expression's address, @var{regex_size} is its
1625 length, and @var{pattern_buffer} is the pattern buffer's address.
1627 If @code{re_compile_pattern} successfully compiles the regular
1628 expression, it returns zero and sets @code{*@var{pattern_buffer}} to the
1629 compiled pattern. It sets the pattern buffer's fields as follows:
1633 @vindex buffer @r{field, set by @code{re_compile_pattern}}
1634 to the compiled pattern.
1637 @vindex used @r{field, set by @code{re_compile_pattern}}
1638 to the number of bytes the compiled pattern in @code{buffer} occupies.
1641 @vindex syntax @r{field, set by @code{re_compile_pattern}}
1642 to the current value of @code{re_syntax_options}.
1645 @vindex re_nsub @r{field, set by @code{re_compile_pattern}}
1646 to the number of subexpressions in @var{regex}.
1648 @item fastmap_accurate
1649 @vindex fastmap_accurate @r{field, set by @code{re_compile_pattern}}
1650 to zero on the theory that the pattern you're compiling is different
1651 than the one previously compiled into @code{buffer}; in that case (since
1652 you can't make a fastmap without a compiled pattern),
1653 @code{fastmap} would either contain an incompatible fastmap, or nothing
1659 If @code{re_compile_pattern} can't compile @var{regex}, it returns an
1660 error string corresponding to one of the errors listed in @ref{POSIX
1661 Regular Expression Compiling}.
1665 @subsection GNU Matching
1667 @cindex matching with GNU functions
1669 Matching the @sc{gnu} way means trying to match as much of a string as
1670 possible starting at a position within it you specify. Once you've compiled
1671 a pattern into a pattern buffer (@pxref{GNU Regular Expression
1672 Compiling}), you can ask the matcher to match that pattern against a
1678 re_match (struct re_pattern_buffer *@var{pattern_buffer},
1679 const char *@var{string}, const int @var{size},
1680 const int @var{start}, struct re_registers *@var{regs})
1684 @var{pattern_buffer} is the address of a pattern buffer containing a
1685 compiled pattern. @var{string} is the string you want to match; it can
1686 contain newline and null characters. @var{size} is the length of that
1687 string. @var{start} is the string index at which you want to
1688 begin matching; the first character of @var{string} is at index zero.
1689 @xref{Using Registers}, for a explanation of @var{regs}; you can safely
1692 @code{re_match} matches the regular expression in @var{pattern_buffer}
1693 against the string @var{string} according to the syntax in
1694 @var{pattern_buffers}'s @code{syntax} field. (@xref{GNU Regular
1695 Expression Compiling}, for how to set it.) The function returns
1696 @math{-1} if the compiled pattern does not match any part of
1697 @var{string} and @math{-2} if an internal error happens; otherwise, it
1698 returns how many (possibly zero) characters of @var{string} the pattern
1701 An example: suppose @var{pattern_buffer} points to a pattern buffer
1702 containing the compiled pattern for @samp{a*}, and @var{string} points
1703 to @samp{aaaaab} (whereupon @var{size} should be 6). Then if @var{start}
1704 is 2, @code{re_match} returns 3, i.e., @samp{a*} would have matched the
1705 last three @samp{a}s in @var{string}. If @var{start} is 0,
1706 @code{re_match} returns 5, i.e., @samp{a*} would have matched all the
1707 @samp{a}s in @var{string}. If @var{start} is either 5 or 6, it returns
1710 If @var{start} is not between zero and @var{size}, then
1711 @code{re_match} returns @math{-1}.
1715 @subsection GNU Searching
1717 @cindex searching with GNU functions
1719 @dfn{Searching} means trying to match starting at successive positions
1720 within a string. The function @code{re_search} does this.
1722 Before calling @code{re_search}, you must compile your regular
1723 expression. @xref{GNU Regular Expression Compiling}.
1725 Here is the function declaration:
1730 re_search (struct re_pattern_buffer *@var{pattern_buffer},
1731 const char *@var{string}, const int @var{size},
1732 const int @var{start}, const int @var{range},
1733 struct re_registers *@var{regs})
1737 @vindex start @r{argument to @code{re_search}}
1738 @vindex range @r{argument to @code{re_search}}
1739 whose arguments are the same as those to @code{re_match} (@pxref{GNU
1740 Matching}) except that the two arguments @var{start} and @var{range}
1741 replace @code{re_match}'s argument @var{start}.
1743 If @var{range} is positive, then @code{re_search} attempts a match
1744 starting first at index @var{start}, then at @math{@var{start} + 1} if
1745 that fails, and so on, up to @math{@var{start} + @var{range}}; if
1746 @var{range} is negative, then it attempts a match starting first at
1747 index @var{start}, then at @math{@var{start} -1} if that fails, and so
1750 If @var{start} is not between zero and @var{size}, then @code{re_search}
1751 returns @math{-1}. When @var{range} is positive, @code{re_search}
1752 adjusts @var{range} so that @math{@var{start} + @var{range} - 1} is
1753 between zero and @var{size}, if necessary; that way it won't search
1754 outside of @var{string}. Similarly, when @var{range} is negative,
1755 @code{re_search} adjusts @var{range} so that @math{@var{start} +
1756 @var{range} + 1} is between zero and @var{size}, if necessary.
1758 If the @code{fastmap} field of @var{pattern_buffer} is zero,
1759 @code{re_search} matches starting at consecutive positions; otherwise,
1760 it uses @code{fastmap} to make the search more efficient.
1761 @xref{Searching with Fastmaps}.
1763 If no match is found, @code{re_search} returns @math{-1}. If
1764 a match is found, it returns the index where the match began. If an
1765 internal error happens, it returns @math{-2}.
1768 @node Matching/Searching with Split Data
1769 @subsection Matching and Searching with Split Data
1771 Using the functions @code{re_match_2} and @code{re_search_2}, you can
1772 match or search in data that is divided into two strings.
1779 re_match_2 (struct re_pattern_buffer *@var{buffer},
1780 const char *@var{string1}, const int @var{size1},
1781 const char *@var{string2}, const int @var{size2},
1782 const int @var{start},
1783 struct re_registers *@var{regs},
1784 const int @var{stop})
1788 is similar to @code{re_match} (@pxref{GNU Matching}) except that you
1789 pass @emph{two} data strings and sizes, and an index @var{stop} beyond
1790 which you don't want the matcher to try matching. As with
1791 @code{re_match}, if it succeeds, @code{re_match_2} returns how many
1792 characters of @var{string} it matched. Regard @var{string1} and
1793 @var{string2} as concatenated when you set the arguments @var{start} and
1794 @var{stop} and use the contents of @var{regs}; @code{re_match_2} never
1795 returns a value larger than @math{@var{size1} + @var{size2}}.
1802 re_search_2 (struct re_pattern_buffer *@var{buffer},
1803 const char *@var{string1}, const int @var{size1},
1804 const char *@var{string2}, const int @var{size2},
1805 const int @var{start}, const int @var{range},
1806 struct re_registers *@var{regs},
1807 const int @var{stop})
1811 is similarly related to @code{re_search}.
1814 @node Searching with Fastmaps
1815 @subsection Searching with Fastmaps
1818 If you're searching through a long string, you should use a fastmap.
1819 Without one, the searcher tries to match at consecutive positions in the
1820 string. Generally, most of the characters in the string could not start
1821 a match. It takes much longer to try matching at a given position in the
1822 string than it does to check in a table whether or not the character at
1823 that position could start a match. A @dfn{fastmap} is such a table.
1825 More specifically, a fastmap is an array indexed by the characters in
1826 your character set. Under the @sc{ascii} encoding, therefore, a fastmap
1827 has 256 elements. If you want the searcher to use a fastmap with a
1828 given pattern buffer, you must allocate the array and assign the array's
1829 address to the pattern buffer's @code{fastmap} field. You either can
1830 compile the fastmap yourself or have @code{re_search} do it for you;
1831 when @code{fastmap} is nonzero, it automatically compiles a fastmap the
1832 first time you search using a particular compiled pattern.
1834 To compile a fastmap yourself, use:
1836 @findex re_compile_fastmap
1839 re_compile_fastmap (struct re_pattern_buffer *@var{pattern_buffer})
1843 @var{pattern_buffer} is the address of a pattern buffer. If the
1844 character @var{c} could start a match for the pattern,
1845 @code{re_compile_fastmap} makes
1846 @code{@var{pattern_buffer}->fastmap[@var{c}]} nonzero. It returns
1847 @math{0} if it can compile a fastmap and @math{-2} if there is an
1848 internal error. For example, if @samp{|} is the alternation operator
1849 and @var{pattern_buffer} holds the compiled pattern for @samp{a|b}, then
1850 @code{re_compile_fastmap} sets @code{fastmap['a']} and
1851 @code{fastmap['b']} (and no others).
1853 @code{re_search} uses a fastmap as it moves along in the string: it
1854 checks the string's characters until it finds one that's in the fastmap.
1855 Then it tries matching at that character. If the match fails, it
1856 repeats the process. So, by using a fastmap, @code{re_search} doesn't
1857 waste time trying to match at positions in the string that couldn't
1860 If you don't want @code{re_search} to use a fastmap,
1861 store zero in the @code{fastmap} field of the pattern buffer before
1862 calling @code{re_search}.
1864 Once you've initialized a pattern buffer's @code{fastmap} field, you
1865 need never do so again---even if you compile a new pattern in
1866 it---provided the way the field is set still reflects whether or not you
1867 want a fastmap. @code{re_search} will still either do nothing if
1868 @code{fastmap} is null or, if it isn't, compile a new fastmap for the
1871 @node GNU Translate Tables
1872 @subsection GNU Translate Tables
1874 If you set the @code{translate} field of a pattern buffer to a translate
1875 table, then the @sc{gnu} Regex functions to which you've passed that
1876 pattern buffer use it to apply a simple transformation
1877 to all the regular expression and string characters at which they look.
1879 A @dfn{translate table} is an array indexed by the characters in your
1880 character set. Under the @sc{ascii} encoding, therefore, a translate
1881 table has 256 elements. The array's elements are also characters in
1882 your character set. When the Regex functions see a character @var{c},
1883 they use @code{translate[@var{c}]} in its place, with one exception: the
1884 character after a @samp{\} is not translated. (This ensures that, the
1885 operators, e.g., @samp{\B} and @samp{\b}, are always distinguishable.)
1887 For example, a table that maps all lowercase letters to the
1888 corresponding uppercase ones would cause the matcher to ignore
1889 differences in case.@footnote{A table that maps all uppercase letters to
1890 the corresponding lowercase ones would work just as well for this
1891 purpose.} Such a table would map all characters except lowercase letters
1892 to themselves, and lowercase letters to the corresponding uppercase
1893 ones. Under the @sc{ascii} encoding, here's how you could initialize
1894 such a table (we'll call it @code{case_fold}):
1897 for (i = 0; i < 256; i++)
1899 for (i = 'a'; i <= 'z'; i++)
1900 case_fold[i] = i - ('a' - 'A');
1903 You tell Regex to use a translate table on a given pattern buffer by
1904 assigning that table's address to the @code{translate} field of that
1905 buffer. If you don't want Regex to do any translation, put zero into
1906 this field. You'll get weird results if you change the table's contents
1907 anytime between compiling the pattern buffer, compiling its fastmap, and
1908 matching or searching with the pattern buffer.
1910 @node Using Registers
1911 @subsection Using Registers
1913 A group in a regular expression can match a (posssibly empty) substring
1914 of the string that regular expression as a whole matched. The matcher
1915 remembers the beginning and end of the substring matched by
1918 To find out what they matched, pass a nonzero @var{regs} argument to a
1919 @sc{gnu} matching or searching function (@pxref{GNU Matching} and
1920 @ref{GNU Searching}), i.e., the address of a structure of this type, as
1921 defined in @file{regex.h}:
1923 @c We don't bother to include this directly from regex.h,
1924 @c since it changes so rarely.
1926 @tindex re_registers
1927 @vindex num_regs @r{in @code{struct re_registers}}
1928 @vindex start @r{in @code{struct re_registers}}
1929 @vindex end @r{in @code{struct re_registers}}
1938 Except for (possibly) the @var{num_regs}'th element (see below), the
1939 @var{i}th element of the @code{start} and @code{end} arrays records
1940 information about the @var{i}th group in the pattern. (They're declared
1941 as C pointers, but this is only because not all C compilers accept
1942 zero-length arrays; conceptually, it is simplest to think of them as
1945 The @code{start} and @code{end} arrays are allocated in various ways,
1946 depending on the value of the @code{regs_allocated}
1947 @vindex regs_allocated
1948 field in the pattern buffer passed to the matcher.
1950 The simplest and perhaps most useful is to let the matcher (re)allocate
1951 enough space to record information for all the groups in the regular
1952 expression. If @code{regs_allocated} is @code{REGS_UNALLOCATED},
1953 @vindex REGS_UNALLOCATED
1954 the matcher allocates @math{1 + @var{re_nsub}} (another field in the
1955 pattern buffer; @pxref{GNU Pattern Buffers}). The extra element is set
1956 to @math{-1}, and sets @code{regs_allocated} to @code{REGS_REALLOCATE}.
1957 @vindex REGS_REALLOCATE
1958 Then on subsequent calls with the same pattern buffer and @var{regs}
1959 arguments, the matcher reallocates more space if necessary.
1961 It would perhaps be more logical to make the @code{regs_allocated} field
1962 part of the @code{re_registers} structure, instead of part of the
1963 pattern buffer. But in that case the caller would be forced to
1964 initialize the structure before passing it. Much existing code doesn't
1965 do this initialization, and it's arguably better to avoid it anyway.
1967 @code{re_compile_pattern} sets @code{regs_allocated} to
1968 @code{REGS_UNALLOCATED},
1969 so if you use the GNU regular expression
1970 functions, you get this behavior by default.
1972 xx document re_set_registers
1974 @sc{posix}, on the other hand, requires a different interface: the
1975 caller is supposed to pass in a fixed-length array which the matcher
1976 fills. Therefore, if @code{regs_allocated} is @code{REGS_FIXED}
1978 the matcher simply fills that array.
1980 The following examples illustrate the information recorded in the
1981 @code{re_registers} structure. (In all of them, @samp{(} represents the
1982 open-group and @samp{)} the close-group operator. The first character
1983 in the string @var{string} is at index 0.)
1985 @c xx i'm not sure this is all true anymore.
1990 If the regular expression has an @w{@var{i}-th}
1991 group not contained within another group that matches a
1992 substring of @var{string}, then the function sets
1993 @code{@w{@var{regs}->}start[@var{i}]} to the index in @var{string} where
1994 the substring matched by the @w{@var{i}-th} group begins, and
1995 @code{@w{@var{regs}->}end[@var{i}]} to the index just beyond that
1996 substring's end. The function sets @code{@w{@var{regs}->}start[0]} and
1997 @code{@w{@var{regs}->}end[0]} to analogous information about the entire
2000 For example, when you match @samp{((a)(b))} against @samp{ab}, you get:
2004 0 in @code{@w{@var{regs}->}start[0]} and 2 in @code{@w{@var{regs}->}end[0]}
2007 0 in @code{@w{@var{regs}->}start[1]} and 2 in @code{@w{@var{regs}->}end[1]}
2010 0 in @code{@w{@var{regs}->}start[2]} and 1 in @code{@w{@var{regs}->}end[2]}
2013 1 in @code{@w{@var{regs}->}start[3]} and 2 in @code{@w{@var{regs}->}end[3]}
2017 If a group matches more than once (as it might if followed by,
2018 e.g., a repetition operator), then the function reports the information
2019 about what the group @emph{last} matched.
2021 For example, when you match the pattern @samp{(a)*} against the string
2026 0 in @code{@w{@var{regs}->}start[0]} and 2 in @code{@w{@var{regs}->}end[0]}
2029 1 in @code{@w{@var{regs}->}start[1]} and 2 in @code{@w{@var{regs}->}end[1]}
2033 If the @w{@var{i}-th} group does not participate in a
2034 successful match, e.g., it is an alternative not taken or a
2035 repetition operator allows zero repetitions of it, then the function
2036 sets @code{@w{@var{regs}->}start[@var{i}]} and
2037 @code{@w{@var{regs}->}end[@var{i}]} to @math{-1}.
2039 For example, when you match the pattern @samp{(a)*b} against
2040 the string @samp{b}, you get:
2044 0 in @code{@w{@var{regs}->}start[0]} and 1 in @code{@w{@var{regs}->}end[0]}
2047 @math{-1} in @code{@w{@var{regs}->}start[1]} and @math{-1} in @code{@w{@var{regs}->}end[1]}
2051 If the @w{@var{i}-th} group matches a zero-length string, then the
2052 function sets @code{@w{@var{regs}->}start[@var{i}]} and
2053 @code{@w{@var{regs}->}end[@var{i}]} to the index just beyond that
2056 For example, when you match the pattern @samp{(a*)b} against the string
2061 0 in @code{@w{@var{regs}->}start[0]} and 1 in @code{@w{@var{regs}->}end[0]}
2064 0 in @code{@w{@var{regs}->}start[1]} and 0 in @code{@w{@var{regs}->}end[1]}
2068 The function sets @code{@w{@var{regs}->}start[0]} and
2069 @code{@w{@var{regs}->}end[0]} to analogous information about the entire
2072 For example, when you match the pattern @samp{(a*)} against the empty
2077 0 in @code{@w{@var{regs}->}start[0]} and 0 in @code{@w{@var{regs}->}end[0]}
2080 0 in @code{@w{@var{regs}->}start[1]} and 0 in @code{@w{@var{regs}->}end[1]}
2085 If an @w{@var{i}-th} group contains a @w{@var{j}-th} group
2086 in turn not contained within any other group within group @var{i} and
2087 the function reports a match of the @w{@var{i}-th} group, then it
2088 records in @code{@w{@var{regs}->}start[@var{j}]} and
2089 @code{@w{@var{regs}->}end[@var{j}]} the last match (if it matched) of
2090 the @w{@var{j}-th} group.
2092 For example, when you match the pattern @samp{((a*)b)*} against the
2093 string @samp{abb}, @w{group 2} last matches the empty string, so you
2094 get what it previously matched:
2098 0 in @code{@w{@var{regs}->}start[0]} and 3 in @code{@w{@var{regs}->}end[0]}
2101 2 in @code{@w{@var{regs}->}start[1]} and 3 in @code{@w{@var{regs}->}end[1]}
2104 2 in @code{@w{@var{regs}->}start[2]} and 2 in @code{@w{@var{regs}->}end[2]}
2107 When you match the pattern @samp{((a)*b)*} against the string
2108 @samp{abb}, @w{group 2} doesn't participate in the last match, so you
2113 0 in @code{@w{@var{regs}->}start[0]} and 3 in @code{@w{@var{regs}->}end[0]}
2116 2 in @code{@w{@var{regs}->}start[1]} and 3 in @code{@w{@var{regs}->}end[1]}
2119 0 in @code{@w{@var{regs}->}start[2]} and 1 in @code{@w{@var{regs}->}end[2]}
2123 If an @w{@var{i}-th} group contains a @w{@var{j}-th} group
2124 in turn not contained within any other group within group @var{i}
2125 and the function sets
2126 @code{@w{@var{regs}->}start[@var{i}]} and
2127 @code{@w{@var{regs}->}end[@var{i}]} to @math{-1}, then it also sets
2128 @code{@w{@var{regs}->}start[@var{j}]} and
2129 @code{@w{@var{regs}->}end[@var{j}]} to @math{-1}.
2131 For example, when you match the pattern @samp{((a)*b)*c} against the
2132 string @samp{c}, you get:
2136 0 in @code{@w{@var{regs}->}start[0]} and 1 in @code{@w{@var{regs}->}end[0]}
2139 @math{-1} in @code{@w{@var{regs}->}start[1]} and @math{-1} in @code{@w{@var{regs}->}end[1]}
2142 @math{-1} in @code{@w{@var{regs}->}start[2]} and @math{-1} in @code{@w{@var{regs}->}end[2]}
2147 @node Freeing GNU Pattern Buffers
2148 @subsection Freeing GNU Pattern Buffers
2150 To free any allocated fields of a pattern buffer, you can use the
2151 @sc{posix} function described in @ref{Freeing POSIX Pattern Buffers},
2152 since the type @code{regex_t}---the type for @sc{posix} pattern
2153 buffers---is equivalent to the type @code{re_pattern_buffer}. After
2154 freeing a pattern buffer, you need to again compile a regular expression
2155 in it (@pxref{GNU Regular Expression Compiling}) before passing it to
2156 a matching or searching function.
2159 @node POSIX Regex Functions
2160 @section POSIX Regex Functions
2162 If you're writing code that has to be @sc{posix} compatible, you'll need
2163 to use these functions. Their interfaces are as specified by @sc{posix},
2167 * POSIX Pattern Buffers:: The regex_t type.
2168 * POSIX Regular Expression Compiling:: regcomp ()
2169 * POSIX Matching:: regexec ()
2170 * Reporting Errors:: regerror ()
2171 * Using Byte Offsets:: The regmatch_t type.
2172 * Freeing POSIX Pattern Buffers:: regfree ()
2176 @node POSIX Pattern Buffers
2177 @subsection POSIX Pattern Buffers
2179 To compile or match a given regular expression the @sc{posix} way, you
2180 must supply a pattern buffer exactly the way you do for @sc{gnu}
2181 (@pxref{GNU Pattern Buffers}). @sc{posix} pattern buffers have type
2182 @code{regex_t}, which is equivalent to the @sc{gnu} pattern buffer
2183 type @code{re_pattern_buffer}.
2186 @node POSIX Regular Expression Compiling
2187 @subsection POSIX Regular Expression Compiling
2189 With @sc{posix}, you can only search for a given regular expression; you
2190 can't match it. To do this, you must first compile it in a
2191 pattern buffer, using @code{regcomp}.
2194 Before calling @code{regcomp}, you must initialize this pattern buffer
2195 as you do for @sc{gnu} (@pxref{GNU Regular Expression Compiling}). See
2196 below, however, for how to choose a syntax with which to compile.
2199 To compile a pattern buffer, use:
2204 regcomp (regex_t *@var{preg}, const char *@var{regex}, int @var{cflags})
2208 @var{preg} is the initialized pattern buffer's address, @var{regex} is
2209 the regular expression's address, and @var{cflags} is the compilation
2210 flags, which Regex considers as a collection of bits. Here are the
2211 valid bits, as defined in @file{regex.h}:
2216 @vindex REG_EXTENDED
2217 says to use @sc{posix} Extended Regular Expression syntax; if this isn't
2218 set, then says to use @sc{posix} Basic Regular Expression syntax.
2219 @code{regcomp} sets @var{preg}'s @code{syntax} field accordingly.
2223 @cindex ignoring case
2224 says to ignore case; @code{regcomp} sets @var{preg}'s @code{translate}
2225 field to a translate table which ignores case, replacing anything you've
2230 says to set @var{preg}'s @code{no_sub} field; @pxref{POSIX Matching},
2231 for what this means.
2240 match-any-character operator (@pxref{Match-any-character
2241 Operator}) doesn't match a newline.
2244 nonmatching list not containing a newline (@pxref{List
2245 Operators}) matches a newline.
2248 match-beginning-of-line operator (@pxref{Match-beginning-of-line
2249 Operator}) matches the empty string immediately after a newline,
2250 regardless of how @code{REG_NOTBOL} is set (@pxref{POSIX Matching}, for
2251 an explanation of @code{REG_NOTBOL}).
2254 match-end-of-line operator (@pxref{Match-beginning-of-line
2255 Operator}) matches the empty string immediately before a newline,
2256 regardless of how @code{REG_NOTEOL} is set (@pxref{POSIX Matching},
2257 for an explanation of @code{REG_NOTEOL}).
2263 If @code{regcomp} successfully compiles the regular expression, it
2264 returns zero and sets @code{*@var{pattern_buffer}} to the compiled
2265 pattern. Except for @code{syntax} (which it sets as explained above), it
2266 also sets the same fields the same way as does the @sc{gnu} compiling
2267 function (@pxref{GNU Regular Expression Compiling}).
2269 If @code{regcomp} can't compile the regular expression, it returns one
2270 of the error codes listed here. (Except when noted differently, the
2271 syntax of in all examples below is basic regular expression syntax.)
2275 @comment repetitions
2277 For example, the consecutive repetition operators @samp{**} in
2278 @samp{a**} are invalid. As another example, if the syntax is extended
2279 regular expression syntax, then the repetition operator @samp{*} with
2280 nothing on which to operate in @samp{*} is invalid.
2283 For example, the @var{count} @samp{-1} in @samp{a\@{-1} is invalid.
2286 For example, @samp{a\@{1} is missing a close-interval operator.
2290 For example, @samp{[a} is missing a close-list operator.
2293 For example, the range ending point @samp{z} that collates lower than
2294 does its starting point @samp{a} in @samp{[z-a]} is invalid. Also, the
2295 range with the character class @samp{[:alpha:]} as its starting point in
2296 @samp{[[:alpha:]-|]}.
2299 For example, the character class name @samp{foo} in @samp{[[:foo:]} is
2304 For example, @samp{a\)} is missing an open-group operator and @samp{\(a}
2305 is missing a close-group operator.
2308 For example, the back reference @samp{\2} that refers to a nonexistent
2309 subexpression in @samp{\(a\)\2} is invalid.
2311 @comment unfinished business
2314 Returned when a regular expression causes no other more specific error.
2317 For example, the trailing backslash @samp{\} in @samp{a\} is invalid, as is the
2320 @comment kitchen sink
2322 For example, in the extended regular expression syntax, the empty group
2323 @samp{()} in @samp{a()b} is invalid.
2327 Returned when a regular expression needs a pattern buffer larger than
2331 Returned when a regular expression makes Regex to run out of memory.
2336 @node POSIX Matching
2337 @subsection POSIX Matching
2339 Matching the @sc{posix} way means trying to match a null-terminated
2340 string starting at its first character. Once you've compiled a pattern
2341 into a pattern buffer (@pxref{POSIX Regular Expression Compiling}), you
2342 can ask the matcher to match that pattern against a string using:
2347 regexec (const regex_t *@var{preg}, const char *@var{string},
2348 size_t @var{nmatch}, regmatch_t @var{pmatch}[], int @var{eflags})
2352 @var{preg} is the address of a pattern buffer for a compiled pattern.
2353 @var{string} is the string you want to match.
2355 @xref{Using Byte Offsets}, for an explanation of @var{pmatch}. If you
2356 pass zero for @var{nmatch} or you compiled @var{preg} with the
2357 compilation flag @code{REG_NOSUB} set, then @code{regexec} will ignore
2358 @var{pmatch}; otherwise, you must allocate it to have at least
2359 @var{nmatch} elements. @code{regexec} will record @var{nmatch} byte
2360 offsets in @var{pmatch}, and set to @math{-1} any unused elements up to
2361 @math{@var{pmatch}@code{[@var{nmatch}]} - 1}.
2363 @var{eflags} specifies @dfn{execution flags}---namely, the two bits
2364 @code{REG_NOTBOL} and @code{REG_NOTEOL} (defined in @file{regex.h}). If
2365 you set @code{REG_NOTBOL}, then the match-beginning-of-line operator
2366 (@pxref{Match-beginning-of-line Operator}) always fails to match.
2367 This lets you match against pieces of a line, as you would need to if,
2368 say, searching for repeated instances of a given pattern in a line; it
2369 would work correctly for patterns both with and without
2370 match-beginning-of-line operators. @code{REG_NOTEOL} works analogously
2371 for the match-end-of-line operator (@pxref{Match-end-of-line
2372 Operator}); it exists for symmetry.
2374 @code{regexec} tries to find a match for @var{preg} in @var{string}
2375 according to the syntax in @var{preg}'s @code{syntax} field.
2376 (@xref{POSIX Regular Expression Compiling}, for how to set it.) The
2377 function returns zero if the compiled pattern matches @var{string} and
2378 @code{REG_NOMATCH} (defined in @file{regex.h}) if it doesn't.
2380 @node Reporting Errors
2381 @subsection Reporting Errors
2383 If either @code{regcomp} or @code{regexec} fail, they return a nonzero
2384 error code, the possibilities for which are defined in @file{regex.h}.
2385 @xref{POSIX Regular Expression Compiling}, and @ref{POSIX Matching}, for
2386 what these codes mean. To get an error string corresponding to these
2392 regerror (int @var{errcode},
2393 const regex_t *@var{preg},
2395 size_t @var{errbuf_size})
2399 @var{errcode} is an error code, @var{preg} is the address of the pattern
2400 buffer which provoked the error, @var{errbuf} is the error buffer, and
2401 @var{errbuf_size} is @var{errbuf}'s size.
2403 @code{regerror} returns the size in bytes of the error string
2404 corresponding to @var{errcode} (including its terminating null). If
2405 @var{errbuf} and @var{errbuf_size} are nonzero, it also returns in
2406 @var{errbuf} the first @math{@var{errbuf_size} - 1} characters of the
2407 error string, followed by a null.
2408 @var{errbuf_size} must be a nonnegative number less than or equal to the
2409 size in bytes of @var{errbuf}.
2411 You can call @code{regerror} with a null @var{errbuf} and a zero
2412 @var{errbuf_size} to determine how large @var{errbuf} need be to
2413 accommodate @code{regerror}'s error string.
2415 @node Using Byte Offsets
2416 @subsection Using Byte Offsets
2418 In @sc{posix}, variables of type @code{regmatch_t} hold analogous
2419 information, but are not identical to, @sc{gnu}'s registers (@pxref{Using
2420 Registers}). To get information about registers in @sc{posix}, pass to
2421 @code{regexec} a nonzero @var{pmatch} of type @code{regmatch_t}, i.e.,
2422 the address of a structure of this type, defined in
2434 When reading in @ref{Using Registers}, about how the matching function
2435 stores the information into the registers, substitute @var{pmatch} for
2436 @var{regs}, @code{@w{@var{pmatch}[@var{i}]->}rm_so} for
2437 @code{@w{@var{regs}->}start[@var{i}]} and
2438 @code{@w{@var{pmatch}[@var{i}]->}rm_eo} for
2439 @code{@w{@var{regs}->}end[@var{i}]}.
2441 @node Freeing POSIX Pattern Buffers
2442 @subsection Freeing POSIX Pattern Buffers
2444 To free any allocated fields of a pattern buffer, use:
2449 regfree (regex_t *@var{preg})
2453 @var{preg} is the pattern buffer whose allocated fields you want freed.
2454 @code{regfree} also sets @var{preg}'s @code{allocated} and @code{used}
2455 fields to zero. After freeing a pattern buffer, you need to again
2456 compile a regular expression in it (@pxref{POSIX Regular Expression
2457 Compiling}) before passing it to the matching function (@pxref{POSIX
2461 @node BSD Regex Functions
2462 @section BSD Regex Functions
2464 If you're writing code that has to be Berkeley @sc{unix} compatible,
2465 you'll need to use these functions whose interfaces are the same as those
2466 in Berkeley @sc{unix}.
2469 * BSD Regular Expression Compiling:: re_comp ()
2470 * BSD Searching:: re_exec ()
2473 @node BSD Regular Expression Compiling
2474 @subsection BSD Regular Expression Compiling
2476 With Berkeley @sc{unix}, you can only search for a given regular
2477 expression; you can't match one. To search for it, you must first
2478 compile it. Before you compile it, you must indicate the regular
2479 expression syntax you want it compiled according to by setting the
2480 variable @code{re_syntax_options} (declared in @file{regex.h} to some
2481 syntax (@pxref{Regular Expression Syntax}).
2483 To compile a regular expression use:
2488 re_comp (char *@var{regex})
2492 @var{regex} is the address of a null-terminated regular expression.
2493 @code{re_comp} uses an internal pattern buffer, so you can use only the
2494 most recently compiled pattern buffer. This means that if you want to
2495 use a given regular expression that you've already compiled---but it
2496 isn't the latest one you've compiled---you'll have to recompile it. If
2497 you call @code{re_comp} with the null string (@emph{not} the empty
2498 string) as the argument, it doesn't change the contents of the pattern
2501 If @code{re_comp} successfully compiles the regular expression, it
2502 returns zero. If it can't compile the regular expression, it returns
2503 an error string. @code{re_comp}'s error messages are identical to those
2504 of @code{re_compile_pattern} (@pxref{GNU Regular Expression
2508 @subsection BSD Searching
2510 Searching the Berkeley @sc{unix} way means searching in a string
2511 starting at its first character and trying successive positions within
2512 it to find a match. Once you've compiled a pattern using @code{re_comp}
2513 (@pxref{BSD Regular Expression Compiling}), you can ask Regex
2514 to search for that pattern in a string using:
2519 re_exec (char *@var{string})
2523 @var{string} is the address of the null-terminated string in which you
2526 @code{re_exec} returns either 1 for success or 0 for failure. It
2527 automatically uses a @sc{gnu} fastmap (@pxref{Searching with Fastmaps}).