1\input texinfo 2@c %**start of header 3@setfilename regex.info 4@settitle Regex 5@c %**end of header 6 7@c Modified by Akim.Demaille@inf.esnt.fr: 8@c - added @minus to the @itemize that had no arguments 9@c - added the following @direntry 10 11@dircategory C library code 12@direntry 13* Regex: (regex). Regular expression library. 14@end direntry 15 16@c \\{fill-paragraph} works better (for me, anyway) if the text in the 17@c source file isn't indented. 18@paragraphindent 2 19 20@c Define a new index for our magic constants. 21@defcodeindex cn 22 23@c Put everything in one index (arbitrarily chosen to be the concept index). 24@syncodeindex cn cp 25@syncodeindex ky cp 26@syncodeindex pg cp 27@syncodeindex tp cp 28@syncodeindex vr cp 29 30@c Here is what we use in the Info `dir' file: 31@c * Regex: (regex). Regular expression library. 32 33 34@ifinfo 35This file documents the GNU regular expression library. 36 37Copyright (C) 1992, 1993 Free Software Foundation, Inc. 38 39Permission is granted to make and distribute verbatim copies of this 40manual provided the copyright notice and this permission notice are 41preserved on all copies. 42 43@ignore 44Permission is granted to process this file through TeX and print the 45results, provided the printed document carries a copying permission 46notice identical to this one except for the removal of this paragraph 47(this paragraph not being relevant to the printed manual). 48@end ignore 49 50Permission is granted to copy and distribute modified versions of this 51manual under the conditions for verbatim copying, provided also that the 52section entitled ``GNU General Public License'' is included exactly as 53in the original, and provided that the entire resulting derived work is 54distributed under the terms of a permission notice identical to this one. 55 56Permission is granted to copy and distribute translations of this manual 57into another language, under the above conditions for modified versions, 58except that the section entitled ``GNU General Public License'' may be 59included in a translation approved by the Free Software Foundation 60instead of in the original English. 61@end ifinfo 62 63 64@titlepage 65 66@title Regex 67@subtitle edition 0.12a 68@subtitle 19 September 1992 69@author Kathryn A. Hargreaves 70@author Karl Berry 71 72@page 73 74@vskip 0pt plus 1filll 75Copyright @copyright{} 1992 Free Software Foundation. 76 77Permission is granted to make and distribute verbatim copies of this 78manual provided the copyright notice and this permission notice are 79preserved on all copies. 80 81Permission is granted to copy and distribute modified versions of this 82manual under the conditions for verbatim copying, provided also that the 83section entitled ``GNU General Public License'' is included exactly as 84in the original, and provided that the entire resulting derived work is 85distributed under the terms of a permission notice identical to this 86one. 87 88Permission is granted to copy and distribute translations of this manual 89into another language, under the above conditions for modified versions, 90except that the section entitled ``GNU General Public License'' may be 91included in a translation approved by the Free Software Foundation 92instead of in the original English. 93 94@end titlepage 95 96 97@ifinfo 98@node Top, Overview, (dir), (dir) 99@top Regular Expression Library 100 101This manual documents how to program with the GNU regular expression 102library. This is edition 0.12a of the manual, 19 September 1992. 103 104The first part of this master menu lists the major nodes in this Info 105document, including the index. The rest of the menu lists all the 106lower level nodes in the document. 107 108@menu 109* Overview:: 110* Regular Expression Syntax:: 111* Common Operators:: 112* GNU Operators:: 113* GNU Emacs Operators:: 114* What Gets Matched?:: 115* Programming with Regex:: 116* Copying:: Copying and sharing Regex. 117* Index:: General index. 118 --- The Detailed Node Listing --- 119 120Regular Expression Syntax 121 122* Syntax Bits:: 123* Predefined Syntaxes:: 124* Collating Elements vs. Characters:: 125* The Backslash Character:: 126 127Common Operators 128 129* Match-self Operator:: Ordinary characters. 130* Match-any-character Operator:: . 131* Concatenation Operator:: Juxtaposition. 132* Repetition Operators:: * + ? @{@} 133* Alternation Operator:: | 134* List Operators:: [...] [^...] 135* Grouping Operators:: (...) 136* Back-reference Operator:: \digit 137* Anchoring Operators:: ^ $ 138 139Repetition Operators 140 141* Match-zero-or-more Operator:: * 142* Match-one-or-more Operator:: + 143* Match-zero-or-one Operator:: ? 144* Interval Operators:: @{@} 145 146List Operators (@code{[} @dots{} @code{]} and @code{[^} @dots{} @code{]}) 147 148* Character Class Operators:: [:class:] 149* Range Operator:: start-end 150 151Anchoring Operators 152 153* Match-beginning-of-line Operator:: ^ 154* Match-end-of-line Operator:: $ 155 156GNU Operators 157 158* Word Operators:: 159* Buffer Operators:: 160 161Word Operators 162 163* Non-Emacs Syntax Tables:: 164* Match-word-boundary Operator:: \b 165* Match-within-word Operator:: \B 166* Match-beginning-of-word Operator:: \< 167* Match-end-of-word Operator:: \> 168* Match-word-constituent Operator:: \w 169* Match-non-word-constituent Operator:: \W 170 171Buffer Operators 172 173* Match-beginning-of-buffer Operator:: \` 174* Match-end-of-buffer Operator:: \' 175 176GNU Emacs Operators 177 178* Syntactic Class Operators:: 179 180Syntactic Class Operators 181 182* Emacs Syntax Tables:: 183* Match-syntactic-class Operator:: \sCLASS 184* Match-not-syntactic-class Operator:: \SCLASS 185 186Programming with Regex 187 188* GNU Regex Functions:: 189* POSIX Regex Functions:: 190* BSD Regex Functions:: 191 192GNU Regex Functions 193 194* GNU Pattern Buffers:: The re_pattern_buffer type. 195* GNU Regular Expression Compiling:: re_compile_pattern () 196* GNU Matching:: re_match () 197* GNU Searching:: re_search () 198* Matching/Searching with Split Data:: re_match_2 (), re_search_2 () 199* Searching with Fastmaps:: re_compile_fastmap () 200* GNU Translate Tables:: The `translate' field. 201* Using Registers:: The re_registers type and related fns. 202* Freeing GNU Pattern Buffers:: regfree () 203 204POSIX Regex Functions 205 206* POSIX Pattern Buffers:: The regex_t type. 207* POSIX Regular Expression Compiling:: regcomp () 208* POSIX Matching:: regexec () 209* Reporting Errors:: regerror () 210* Using Byte Offsets:: The regmatch_t type. 211* Freeing POSIX Pattern Buffers:: regfree () 212 213BSD Regex Functions 214 215* BSD Regular Expression Compiling:: re_comp () 216* BSD Searching:: re_exec () 217@end menu 218@end ifinfo 219@node Overview, Regular Expression Syntax, Top, Top 220@chapter Overview 221 222A @dfn{regular expression} (or @dfn{regexp}, or @dfn{pattern}) is a text 223string that describes some (mathematical) set of strings. A regexp 224@var{r} @dfn{matches} a string @var{s} if @var{s} is in the set of 225strings described by @var{r}. 226 227Using the Regex library, you can: 228 229@itemize @bullet 230 231@item 232see if a string matches a specified pattern as a whole, and 233 234@item 235search within a string for a substring matching a specified pattern. 236 237@end itemize 238 239Some regular expressions match only one string, i.e., the set they 240describe has only one member. For example, the regular expression 241@samp{foo} matches the string @samp{foo} and no others. Other regular 242expressions match more than one string, i.e., the set they describe has 243more than one member. For example, the regular expression @samp{f*} 244matches the set of strings made up of any number (including zero) of 245@samp{f}s. As you can see, some characters in regular expressions match 246themselves (such as @samp{f}) and some don't (such as @samp{*}); the 247ones that don't match themselves instead let you specify patterns that 248describe many different strings. 249 250To either match or search for a regular expression with the Regex 251library functions, you must first compile it with a Regex pattern 252compiling function. A @dfn{compiled pattern} is a regular expression 253converted to the internal format used by the library functions. Once 254you've compiled a pattern, you can use it for matching or searching any 255number of times. 256 257The Regex library consists of two source files: @file{regex.h} and 258@file{regex.c}. 259@pindex regex.h 260@pindex regex.c 261Regex provides three groups of functions with which you can operate on 262regular expressions. One group---the @sc{gnu} group---is more powerful 263but not completely compatible with the other two, namely the @sc{posix} 264and Berkeley @sc{unix} groups; its interface was designed specifically 265for @sc{gnu}. The other groups have the same interfaces as do the 266regular expression functions in @sc{posix} and Berkeley 267@sc{unix}. 268 269We wrote this chapter with programmers in mind, not users of 270programs---such as Emacs---that use Regex. We describe the Regex 271library in its entirety, not how to write regular expressions that a 272particular program understands. 273 274 275@node Regular Expression Syntax, Common Operators, Overview, Top 276@chapter Regular Expression Syntax 277 278@cindex regular expressions, syntax of 279@cindex syntax of regular expressions 280 281@dfn{Characters} are things you can type. @dfn{Operators} are things in 282a regular expression that match one or more characters. You compose 283regular expressions from operators, which in turn you specify using one 284or more characters. 285 286Most characters represent what we call the match-self operator, i.e., 287they match themselves; we call these characters @dfn{ordinary}. Other 288characters represent either all or parts of fancier operators; e.g., 289@samp{.} represents what we call the match-any-character operator 290(which, no surprise, matches (almost) any character); we call these 291characters @dfn{special}. Two different things determine what 292characters represent what operators: 293 294@enumerate 295@item 296the regular expression syntax your program has told the Regex library to 297recognize, and 298 299@item 300the context of the character in the regular expression. 301@end enumerate 302 303In the following sections, we describe these things in more detail. 304 305@menu 306* Syntax Bits:: 307* Predefined Syntaxes:: 308* Collating Elements vs. Characters:: 309* The Backslash Character:: 310@end menu 311 312 313@node Syntax Bits, Predefined Syntaxes, , Regular Expression Syntax 314@section Syntax Bits 315 316@cindex syntax bits 317 318In any particular syntax for regular expressions, some characters are 319always special, others are sometimes special, and others are never 320special. The particular syntax that Regex recognizes for a given 321regular expression depends on the value in the @code{syntax} field of 322the pattern buffer of that regular expression. 323 324You get a pattern buffer by compiling a regular expression. @xref{GNU 325Pattern Buffers}, and @ref{POSIX Pattern Buffers}, for more information 326on pattern buffers. @xref{GNU Regular Expression Compiling}, @ref{POSIX 327Regular Expression Compiling}, and @ref{BSD Regular Expression 328Compiling}, for more information on compiling. 329 330Regex considers the value of the @code{syntax} field to be a collection 331of bits; we refer to these bits as @dfn{syntax bits}. In most cases, 332they affect what characters represent what operators. We describe the 333meanings of the operators to which we refer in @ref{Common Operators}, 334@ref{GNU Operators}, and @ref{GNU Emacs Operators}. 335 336For reference, here is the complete list of syntax bits, in alphabetical 337order: 338 339@table @code 340 341@cnindex RE_BACKSLASH_ESCAPE_IN_LIST 342@item RE_BACKSLASH_ESCAPE_IN_LISTS 343If this bit is set, then @samp{\} inside a list (@pxref{List Operators} 344quotes (makes ordinary, if it's special) the following character; if 345this bit isn't set, then @samp{\} is an ordinary character inside lists. 346(@xref{The Backslash Character}, for what `\' does outside of lists.) 347 348@cnindex RE_BK_PLUS_QM 349@item RE_BK_PLUS_QM 350If this bit is set, then @samp{\+} represents the match-one-or-more 351operator and @samp{\?} represents the match-zero-or-more operator; if 352this bit isn't set, then @samp{+} represents the match-one-or-more 353operator and @samp{?} represents the match-zero-or-one operator. This 354bit is irrelevant if @code{RE_LIMITED_OPS} is set. 355 356@cnindex RE_CHAR_CLASSES 357@item RE_CHAR_CLASSES 358If this bit is set, then you can use character classes in lists; if this 359bit isn't set, then you can't. 360 361@cnindex RE_CONTEXT_INDEP_ANCHORS 362@item RE_CONTEXT_INDEP_ANCHORS 363If this bit is set, then @samp{^} and @samp{$} are special anywhere outside 364a list; if this bit isn't set, then these characters are special only in 365certain contexts. @xref{Match-beginning-of-line Operator}, and 366@ref{Match-end-of-line Operator}. 367 368@cnindex RE_CONTEXT_INDEP_OPS 369@item RE_CONTEXT_INDEP_OPS 370If this bit is set, then certain characters are special anywhere outside 371a list; if this bit isn't set, then those characters are special only in 372some contexts and are ordinary elsewhere. Specifically, if this bit 373isn't set then @samp{*}, and (if the syntax bit @code{RE_LIMITED_OPS} 374isn't set) @samp{+} and @samp{?} (or @samp{\+} and @samp{\?}, depending 375on the syntax bit @code{RE_BK_PLUS_QM}) represent repetition operators 376only if they're not first in a regular expression or just after an 377open-group or alternation operator. The same holds for @samp{@{} (or 378@samp{\@{}, depending on the syntax bit @code{RE_NO_BK_BRACES}) if 379it is the beginning of a valid interval and the syntax bit 380@code{RE_INTERVALS} is set. 381 382@cnindex RE_CONTEXT_INVALID_OPS 383@item RE_CONTEXT_INVALID_OPS 384If this bit is set, then repetition and alternation operators can't be 385in certain positions within a regular expression. Specifically, the 386regular expression is invalid if it has: 387 388@itemize @bullet 389 390@item 391a repetition operator first in the regular expression or just after a 392match-beginning-of-line, open-group, or alternation operator; or 393 394@item 395an alternation operator first or last in the regular expression, just 396before a match-end-of-line operator, or just after an alternation or 397open-group operator. 398 399@end itemize 400 401If this bit isn't set, then you can put the characters representing the 402repetition and alternation characters anywhere in a regular expression. 403Whether or not they will in fact be operators in certain positions 404depends on other syntax bits. 405 406@cnindex RE_DOT_NEWLINE 407@item RE_DOT_NEWLINE 408If this bit is set, then the match-any-character operator matches 409a newline; if this bit isn't set, then it doesn't. 410 411@cnindex RE_DOT_NOT_NULL 412@item RE_DOT_NOT_NULL 413If this bit is set, then the match-any-character operator doesn't match 414a null character; if this bit isn't set, then it does. 415 416@cnindex RE_INTERVALS 417@item RE_INTERVALS 418If this bit is set, then Regex recognizes interval operators; if this bit 419isn't set, then it doesn't. 420 421@cnindex RE_LIMITED_OPS 422@item RE_LIMITED_OPS 423If this bit is set, then Regex doesn't recognize the match-one-or-more, 424match-zero-or-one or alternation operators; if this bit isn't set, then 425it does. 426 427@cnindex RE_NEWLINE_ALT 428@item RE_NEWLINE_ALT 429If this bit is set, then newline represents the alternation operator; if 430this bit isn't set, then newline is ordinary. 431 432@cnindex RE_NO_BK_BRACES 433@item RE_NO_BK_BRACES 434If this bit is set, then @samp{@{} represents the open-interval operator 435and @samp{@}} represents the close-interval operator; if this bit isn't 436set, then @samp{\@{} represents the open-interval operator and 437@samp{\@}} represents the close-interval operator. This bit is relevant 438only if @code{RE_INTERVALS} is set. 439 440@cnindex RE_NO_BK_PARENS 441@item RE_NO_BK_PARENS 442If this bit is set, then @samp{(} represents the open-group operator and 443@samp{)} represents the close-group operator; if this bit isn't set, then 444@samp{\(} represents the open-group operator and @samp{\)} represents 445the close-group operator. 446 447@cnindex RE_NO_BK_REFS 448@item RE_NO_BK_REFS 449If this bit is set, then Regex doesn't recognize @samp{\}@var{digit} as 450the back reference operator; if this bit isn't set, then it does. 451 452@cnindex RE_NO_BK_VBAR 453@item RE_NO_BK_VBAR 454If this bit is set, then @samp{|} represents the alternation operator; 455if this bit isn't set, then @samp{\|} represents the alternation 456operator. This bit is irrelevant if @code{RE_LIMITED_OPS} is set. 457 458@cnindex RE_NO_EMPTY_RANGES 459@item RE_NO_EMPTY_RANGES 460If this bit is set, then a regular expression with a range whose ending 461point collates lower than its starting point is invalid; if this bit 462isn't set, then Regex considers such a range to be empty. 463 464@cnindex RE_UNMATCHED_RIGHT_PAREN_ORD 465@item RE_UNMATCHED_RIGHT_PAREN_ORD 466If this bit is set and the regular expression has no matching open-group 467operator, then Regex considers what would otherwise be a close-group 468operator (based on how @code{RE_NO_BK_PARENS} is set) to match @samp{)}. 469 470@end table 471 472 473@node Predefined Syntaxes, Collating Elements vs. Characters, Syntax Bits, Regular Expression Syntax 474@section Predefined Syntaxes 475 476If you're programming with Regex, you can set a pattern buffer's 477(@pxref{GNU Pattern Buffers}, and @ref{POSIX Pattern Buffers}) 478@code{syntax} field either to an arbitrary combination of syntax bits 479(@pxref{Syntax Bits}) or else to the configurations defined by Regex. 480These configurations define the syntaxes used by certain 481programs---@sc{gnu} Emacs, 482@cindex Emacs 483@sc{posix} Awk, 484@cindex POSIX Awk 485traditional Awk, 486@cindex Awk 487Grep, 488@cindex Grep 489@cindex Egrep 490Egrep---in addition to syntaxes for @sc{posix} basic and extended 491regular expressions. 492 493The predefined syntaxes--taken directly from @file{regex.h}---are: 494 495@example 496#define RE_SYNTAX_EMACS 0 497 498#define RE_SYNTAX_AWK \ 499 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \ 500 | RE_NO_BK_PARENS | RE_NO_BK_REFS \ 501 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \ 502 | RE_UNMATCHED_RIGHT_PAREN_ORD) 503 504#define RE_SYNTAX_POSIX_AWK \ 505 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS) 506 507#define RE_SYNTAX_GREP \ 508 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \ 509 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \ 510 | RE_NEWLINE_ALT) 511 512#define RE_SYNTAX_EGREP \ 513 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \ 514 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \ 515 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \ 516 | RE_NO_BK_VBAR) 517 518#define RE_SYNTAX_POSIX_EGREP \ 519 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES) 520 521/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */ 522#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC 523 524#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC 525 526/* Syntax bits common to both basic and extended POSIX regex syntax. */ 527#define _RE_SYNTAX_POSIX_COMMON \ 528 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \ 529 | RE_INTERVALS | RE_NO_EMPTY_RANGES) 530 531#define RE_SYNTAX_POSIX_BASIC \ 532 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM) 533 534/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes 535 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this 536 isn't minimal, since other operators, such as \`, aren't disabled. */ 537#define RE_SYNTAX_POSIX_MINIMAL_BASIC \ 538 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS) 539 540#define RE_SYNTAX_POSIX_EXTENDED \ 541 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ 542 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \ 543 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \ 544 | RE_UNMATCHED_RIGHT_PAREN_ORD) 545 546/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS 547 replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */ 548#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \ 549 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ 550 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \ 551 | RE_NO_BK_PARENS | RE_NO_BK_REFS \ 552 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD) 553@end example 554 555@node Collating Elements vs. Characters, The Backslash Character, Predefined Syntaxes, Regular Expression Syntax 556@section Collating Elements vs.@: Characters 557 558@sc{posix} generalizes the notion of a character to that of a 559collating element. It defines a @dfn{collating element} to be ``a 560sequence of one or more bytes defined in the current collating sequence 561as a unit of collation.'' 562 563This generalizes the notion of a character in 564two ways. First, a single character can map into two or more collating 565elements. For example, the German 566@tex 567`\ss' 568@end tex 569@ifinfo 570``es-zet'' 571@end ifinfo 572collates as the collating element @samp{s} followed by another collating 573element @samp{s}. Second, two or more characters can map into one 574collating element. For example, the Spanish @samp{ll} collates after 575@samp{l} and before @samp{m}. 576 577Since @sc{posix}'s ``collating element'' preserves the essential idea of 578a ``character,'' we use the latter, more familiar, term in this document. 579 580@node The Backslash Character, , Collating Elements vs. Characters, Regular Expression Syntax 581@section The Backslash Character 582 583@cindex @samp{\} 584The @samp{\} character has one of four different meanings, depending on 585the context in which you use it and what syntax bits are set 586(@pxref{Syntax Bits}). It can: 1) stand for itself, 2) quote the next 587character, 3) introduce an operator, or 4) do nothing. 588 589@enumerate 590@item 591It stands for itself inside a list 592(@pxref{List Operators}) if the syntax bit 593@code{RE_BACKSLASH_ESCAPE_IN_LISTS} is not set. For example, @samp{[\]} 594would match @samp{\}. 595 596@item 597It quotes (makes ordinary, if it's special) the next character when you 598use it either: 599 600@itemize @bullet 601@item 602outside a list,@footnote{Sometimes 603you don't have to explicitly quote special characters to make 604them ordinary. For instance, most characters lose any special meaning 605inside a list (@pxref{List Operators}). In addition, if the syntax bits 606@code{RE_CONTEXT_INVALID_OPS} and @code{RE_CONTEXT_INDEP_OPS} 607aren't set, then (for historical reasons) the matcher considers special 608characters ordinary if they are in contexts where the operations they 609represent make no sense; for example, then the match-zero-or-more 610operator (represented by @samp{*}) matches itself in the regular 611expression @samp{*foo} because there is no preceding expression on which 612it can operate. It is poor practice, however, to depend on this 613behavior; if you want a special character to be ordinary outside a list, 614it's better to always quote it, regardless.} or 615 616@item 617inside a list and the syntax bit @code{RE_BACKSLASH_ESCAPE_IN_LISTS} is set. 618 619@end itemize 620 621@item 622It introduces an operator when followed by certain ordinary 623characters---sometimes only when certain syntax bits are set. See the 624cases @code{RE_BK_PLUS_QM}, @code{RE_NO_BK_BRACES}, @code{RE_NO_BK_VAR}, 625@code{RE_NO_BK_PARENS}, @code{RE_NO_BK_REF} in @ref{Syntax Bits}. Also: 626 627@itemize @bullet 628@item 629@samp{\b} represents the match-word-boundary operator 630(@pxref{Match-word-boundary Operator}). 631 632@item 633@samp{\B} represents the match-within-word operator 634(@pxref{Match-within-word Operator}). 635 636@item 637@samp{\<} represents the match-beginning-of-word operator @* 638(@pxref{Match-beginning-of-word Operator}). 639 640@item 641@samp{\>} represents the match-end-of-word operator 642(@pxref{Match-end-of-word Operator}). 643 644@item 645@samp{\w} represents the match-word-constituent operator 646(@pxref{Match-word-constituent Operator}). 647 648@item 649@samp{\W} represents the match-non-word-constituent operator 650(@pxref{Match-non-word-constituent Operator}). 651 652@item 653@samp{\`} represents the match-beginning-of-buffer 654operator and @samp{\'} represents the match-end-of-buffer operator 655(@pxref{Buffer Operators}). 656 657@item 658If Regex was compiled with the C preprocessor symbol @code{emacs} 659defined, then @samp{\s@var{class}} represents the match-syntactic-class 660operator and @samp{\S@var{class}} represents the 661match-not-syntactic-class operator (@pxref{Syntactic Class Operators}). 662 663@end itemize 664 665@item 666In all other cases, Regex ignores @samp{\}. For example, 667@samp{\n} matches @samp{n}. 668 669@end enumerate 670 671@node Common Operators, GNU Operators, Regular Expression Syntax, Top 672@chapter Common Operators 673 674You compose regular expressions from operators. In the following 675sections, we describe the regular expression operators specified by 676@sc{posix}; @sc{gnu} also uses these. Most operators have more than one 677representation as characters. @xref{Regular Expression Syntax}, for 678what characters represent what operators under what circumstances. 679 680For most operators that can be represented in two ways, one 681representation is a single character and the other is that character 682preceded by @samp{\}. For example, either @samp{(} or @samp{\(} 683represents the open-group operator. Which one does depends on the 684setting of a syntax bit, in this case @code{RE_NO_BK_PARENS}. Why is 685this so? Historical reasons dictate some of the varying 686representations, while @sc{posix} dictates others. 687 688Finally, almost all characters lose any special meaning inside a list 689(@pxref{List Operators}). 690 691@menu 692* Match-self Operator:: Ordinary characters. 693* Match-any-character Operator:: . 694* Concatenation Operator:: Juxtaposition. 695* Repetition Operators:: * + ? @{@} 696* Alternation Operator:: | 697* List Operators:: [...] [^...] 698* Grouping Operators:: (...) 699* Back-reference Operator:: \digit 700* Anchoring Operators:: ^ $ 701@end menu 702 703@node Match-self Operator, Match-any-character Operator, , Common Operators 704@section The Match-self Operator (@var{ordinary character}) 705 706This operator matches the character itself. All ordinary characters 707(@pxref{Regular Expression Syntax}) represent this operator. For 708example, @samp{f} is always an ordinary character, so the regular 709expression @samp{f} matches only the string @samp{f}. In 710particular, it does @emph{not} match the string @samp{ff}. 711 712@node Match-any-character Operator, Concatenation Operator, Match-self Operator, Common Operators 713@section The Match-any-character Operator (@code{.}) 714 715@cindex @samp{.} 716 717This operator matches any single printing or nonprinting character 718except it won't match a: 719 720@table @asis 721@item newline 722if the syntax bit @code{RE_DOT_NEWLINE} isn't set. 723 724@item null 725if the syntax bit @code{RE_DOT_NOT_NULL} is set. 726 727@end table 728 729The @samp{.} (period) character represents this operator. For example, 730@samp{a.b} matches any three-character string beginning with @samp{a} 731and ending with @samp{b}. 732 733@node Concatenation Operator, Repetition Operators, Match-any-character Operator, Common Operators 734@section The Concatenation Operator 735 736This operator concatenates two regular expressions @var{a} and @var{b}. 737No character represents this operator; you simply put @var{b} after 738@var{a}. The result is a regular expression that will match a string if 739@var{a} matches its first part and @var{b} matches the rest. For 740example, @samp{xy} (two match-self operators) matches @samp{xy}. 741 742@node Repetition Operators, Alternation Operator, Concatenation Operator, Common Operators 743@section Repetition Operators 744 745Repetition operators repeat the preceding regular expression a specified 746number of times. 747 748@menu 749* Match-zero-or-more Operator:: * 750* Match-one-or-more Operator:: + 751* Match-zero-or-one Operator:: ? 752* Interval Operators:: @{@} 753@end menu 754 755@node Match-zero-or-more Operator, Match-one-or-more Operator, , Repetition Operators 756@subsection The Match-zero-or-more Operator (@code{*}) 757 758@cindex @samp{*} 759 760This operator repeats the smallest possible preceding regular expression 761as many times as necessary (including zero) to match the pattern. 762@samp{*} represents this operator. For example, @samp{o*} 763matches any string made up of zero or more @samp{o}s. Since this 764operator operates on the smallest preceding regular expression, 765@samp{fo*} has a repeating @samp{o}, not a repeating @samp{fo}. So, 766@samp{fo*} matches @samp{f}, @samp{fo}, @samp{foo}, and so on. 767 768Since the match-zero-or-more operator is a suffix operator, it may be 769useless as such when no regular expression precedes it. This is the 770case when it: 771 772@itemize @bullet 773@item 774is first in a regular expression, or 775 776@item 777follows a match-beginning-of-line, open-group, or alternation 778operator. 779 780@end itemize 781 782@noindent 783Three different things can happen in these cases: 784 785@enumerate 786@item 787If the syntax bit @code{RE_CONTEXT_INVALID_OPS} is set, then the 788regular expression is invalid. 789 790@item 791If @code{RE_CONTEXT_INVALID_OPS} isn't set, but 792@code{RE_CONTEXT_INDEP_OPS} is, then @samp{*} represents the 793match-zero-or-more operator (which then operates on the empty string). 794 795@item 796Otherwise, @samp{*} is ordinary. 797 798@end enumerate 799 800@cindex backtracking 801The matcher processes a match-zero-or-more operator by first matching as 802many repetitions of the smallest preceding regular expression as it can. 803Then it continues to match the rest of the pattern. 804 805If it can't match the rest of the pattern, it backtracks (as many times 806as necessary), each time discarding one of the matches until it can 807either match the entire pattern or be certain that it cannot get a 808match. For example, when matching @samp{ca*ar} against @samp{caaar}, 809the matcher first matches all three @samp{a}s of the string with the 810@samp{a*} of the regular expression. However, it cannot then match the 811final @samp{ar} of the regular expression against the final @samp{r} of 812the string. So it backtracks, discarding the match of the last @samp{a} 813in the string. It can then match the remaining @samp{ar}. 814 815 816@node Match-one-or-more Operator, Match-zero-or-one Operator, Match-zero-or-more Operator, Repetition Operators 817@subsection The Match-one-or-more Operator (@code{+} or @code{\+}) 818 819@cindex @samp{+} 820 821If the syntax bit @code{RE_LIMITED_OPS} is set, then Regex doesn't recognize 822this operator. Otherwise, if the syntax bit @code{RE_BK_PLUS_QM} isn't 823set, then @samp{+} represents this operator; if it is, then @samp{\+} 824does. 825 826This operator is similar to the match-zero-or-more operator except that 827it repeats the preceding regular expression at least once; 828@pxref{Match-zero-or-more Operator}, for what it operates on, how some 829syntax bits affect it, and how Regex backtracks to match it. 830 831For example, supposing that @samp{+} represents the match-one-or-more 832operator; then @samp{ca+r} matches, e.g., @samp{car} and 833@samp{caaaar}, but not @samp{cr}. 834 835@node Match-zero-or-one Operator, Interval Operators, Match-one-or-more Operator, Repetition Operators 836@subsection The Match-zero-or-one Operator (@code{?} or @code{\?}) 837@cindex @samp{?} 838 839If the syntax bit @code{RE_LIMITED_OPS} is set, then Regex doesn't 840recognize this operator. Otherwise, if the syntax bit 841@code{RE_BK_PLUS_QM} isn't set, then @samp{?} represents this operator; 842if it is, then @samp{\?} does. 843 844This operator is similar to the match-zero-or-more operator except that 845it repeats the preceding regular expression once or not at all; 846@pxref{Match-zero-or-more Operator}, to see what it operates on, how 847some syntax bits affect it, and how Regex backtracks to match it. 848 849For example, supposing that @samp{?} represents the match-zero-or-one 850operator; then @samp{ca?r} matches both @samp{car} and @samp{cr}, but 851nothing else. 852 853@node Interval Operators, , Match-zero-or-one Operator, Repetition Operators 854@subsection Interval Operators (@code{@{} @dots{} @code{@}} or @code{\@{} @dots{} @code{\@}}) 855 856@cindex interval expression 857@cindex @samp{@{} 858@cindex @samp{@}} 859@cindex @samp{\@{} 860@cindex @samp{\@}} 861 862If the syntax bit @code{RE_INTERVALS} is set, then Regex recognizes 863@dfn{interval expressions}. They repeat the smallest possible preceding 864regular expression a specified number of times. 865 866If the syntax bit @code{RE_NO_BK_BRACES} is set, @samp{@{} represents 867the @dfn{open-interval operator} and @samp{@}} represents the 868@dfn{close-interval operator} ; otherwise, @samp{\@{} and @samp{\@}} do. 869 870Specifically, supposing that @samp{@{} and @samp{@}} represent the 871open-interval and close-interval operators; then: 872 873@table @code 874@item @{@var{count}@} 875matches exactly @var{count} occurrences of the preceding regular 876expression. 877 878@item @{@var{min},@} 879matches @var{min} or more occurrences of the preceding regular 880expression. 881 882@item @{@var{min}, @var{max}@} 883matches at least @var{min} but no more than @var{max} occurrences of 884the preceding regular expression. 885 886@end table 887 888The interval expression (but not necessarily the regular expression that 889contains it) is invalid if: 890 891@itemize @bullet 892@item 893@var{min} is greater than @var{max}, or 894 895@item 896any of @var{count}, @var{min}, or @var{max} are outside the range 897zero to @code{RE_DUP_MAX} (which symbol @file{regex.h} 898defines). 899 900@end itemize 901 902If the interval expression is invalid and the syntax bit 903@code{RE_NO_BK_BRACES} is set, then Regex considers all the 904characters in the would-be interval to be ordinary. If that bit 905isn't set, then the regular expression is invalid. 906 907If the interval expression is valid but there is no preceding regular 908expression on which to operate, then if the syntax bit 909@code{RE_CONTEXT_INVALID_OPS} is set, the regular expression is invalid. 910If that bit isn't set, then Regex considers all the characters---other 911than backslashes, which it ignores---in the would-be interval to be 912ordinary. 913 914 915@node Alternation Operator, List Operators, Repetition Operators, Common Operators 916@section The Alternation Operator (@code{|} or @code{\|}) 917 918@kindex @samp{|} 919@kindex @samp{\|} 920@cindex alternation operator 921@cindex or operator 922 923If the syntax bit @code{RE_LIMITED_OPS} is set, then Regex doesn't 924recognize this operator. Otherwise, if the syntax bit 925@code{RE_NO_BK_VBAR} is set, then @samp{|} represents this operator; 926otherwise, @samp{\|} does. 927 928Alternatives match one of a choice of regular expressions: 929if you put the character(s) representing the alternation operator between 930any two regular expressions @var{a} and @var{b}, the result matches 931the union of the strings that @var{a} and @var{b} match. For 932example, supposing that @samp{|} is the alternation operator, then 933@samp{foo|bar|quux} would match any of @samp{foo}, @samp{bar} or 934@samp{quux}. 935 936@ignore 937@c Nobody needs to disallow empty alternatives any more. 938If the syntax bit @code{RE_NO_EMPTY_ALTS} is set, then if either of the regular 939expressions @var{a} or @var{b} is empty, the 940regular expression is invalid. More precisely, if this syntax bit is 941set, then the alternation operator can't: 942 943@itemize @bullet 944@item 945be first or last in a regular expression; 946 947@item 948follow either another alternation operator or an open-group operator 949(@pxref{Grouping Operators}); or 950 951@item 952precede a close-group operator. 953 954@end itemize 955 956@noindent 957For example, supposing @samp{(} and @samp{)} represent the open and 958close-group operators, then @samp{|foo}, @samp{foo|}, @samp{foo||bar}, 959@samp{foo(|bar)}, and @samp{(foo|)bar} would all be invalid. 960@end ignore 961 962The alternation operator operates on the @emph{largest} possible 963surrounding regular expressions. (Put another way, it has the lowest 964precedence of any regular expression operator.) 965Thus, the only way you can 966delimit its arguments is to use grouping. For example, if @samp{(} and 967@samp{)} are the open and close-group operators, then @samp{fo(o|b)ar} 968would match either @samp{fooar} or @samp{fobar}. (@samp{foo|bar} would 969match @samp{foo} or @samp{bar}.) 970 971@cindex backtracking 972The matcher usually tries all combinations of alternatives so as to 973match the longest possible string. For example, when matching 974@samp{(fooq|foo)*(qbarquux|bar)} against @samp{fooqbarquux}, it cannot 975take, say, the first (``depth-first'') combination it could match, since 976then it would be content to match just @samp{fooqbar}. 977 978@comment xx something about leftmost-longest 979 980 981@node List Operators, Grouping Operators, Alternation Operator, Common Operators 982@section List Operators (@code{[} @dots{} @code{]} and @code{[^} @dots{} @code{]}) 983 984@cindex matching list 985@cindex @samp{[} 986@cindex @samp{]} 987@cindex @samp{^} 988@cindex @samp{-} 989@cindex @samp{\} 990@cindex @samp{[^} 991@cindex nonmatching list 992@cindex matching newline 993@cindex bracket expression 994 995@dfn{Lists}, also called @dfn{bracket expressions}, are a set of one or 996more items. An @dfn{item} is a character, 997@ignore 998(These get added when they get implemented.) 999a collating symbol, an equivalence class expression, 1000@end ignore 1001a character class expression, or a range expression. The syntax bits 1002affect which kinds of items you can put in a list. We explain the last 1003two items in subsections below. Empty lists are invalid. 1004 1005A @dfn{matching list} matches a single character represented by one of 1006the list items. You form a matching list by enclosing one or more items 1007within an @dfn{open-matching-list operator} (represented by @samp{[}) 1008and a @dfn{close-list operator} (represented by @samp{]}). 1009 1010For example, @samp{[ab]} matches either @samp{a} or @samp{b}. 1011@samp{[ad]*} matches the empty string and any string composed of just 1012@samp{a}s and @samp{d}s in any order. Regex considers invalid a regular 1013expression with a @samp{[} but no matching 1014@samp{]}. 1015 1016@dfn{Nonmatching lists} are similar to matching lists except that they 1017match a single character @emph{not} represented by one of the list 1018items. You use an @dfn{open-nonmatching-list operator} (represented by 1019@samp{[^}@footnote{Regex therefore doesn't consider the @samp{^} to be 1020the first character in the list. If you put a @samp{^} character first 1021in (what you think is) a matching list, you'll turn it into a 1022nonmatching list.}) instead of an open-matching-list operator to start a 1023nonmatching list. 1024 1025For example, @samp{[^ab]} matches any character except @samp{a} or 1026@samp{b}. 1027 1028If the @code{posix_newline} field in the pattern buffer (@pxref{GNU 1029Pattern Buffers} is set, then nonmatching lists do not match a newline. 1030 1031Most characters lose any special meaning inside a list. The special 1032characters inside a list follow. 1033 1034@table @samp 1035@item ] 1036ends the list if it's not the first list item. So, if you want to make 1037the @samp{]} character a list item, you must put it first. 1038 1039@item \ 1040quotes the next character if the syntax bit @code{RE_BACKSLASH_ESCAPE_IN_LISTS} is 1041set. 1042 1043@ignore 1044Put these in if they get implemented. 1045 1046@item [. 1047represents the open-collating-symbol operator (@pxref{Collating Symbol 1048Operators}). 1049 1050@item .] 1051represents the close-collating-symbol operator. 1052 1053@item [= 1054represents the open-equivalence-class operator (@pxref{Equivalence Class 1055Operators}). 1056 1057@item =] 1058represents the close-equivalence-class operator. 1059 1060@end ignore 1061 1062@item [: 1063represents the open-character-class operator (@pxref{Character Class 1064Operators}) if the syntax bit @code{RE_CHAR_CLASSES} is set and what 1065follows is a valid character class expression. 1066 1067@item :] 1068represents the close-character-class operator if the syntax bit 1069@code{RE_CHAR_CLASSES} is set and what precedes it is an 1070open-character-class operator followed by a valid character class name. 1071 1072@item - 1073represents the range operator (@pxref{Range Operator}) if it's 1074not first or last in a list or the ending point of a range. 1075 1076@end table 1077 1078@noindent 1079All other characters are ordinary. For example, @samp{[.*]} matches 1080@samp{.} and @samp{*}. 1081 1082@menu 1083* Character Class Operators:: [:class:] 1084* Range Operator:: start-end 1085@end menu 1086 1087@ignore 1088(If collating symbols and equivalence class expressions get implemented, 1089then add this.) 1090 1091node Collating Symbol Operators 1092subsubsection Collating Symbol Operators (@code{[.} @dots{} @code{.]}) 1093 1094If the syntax bit @code{XX} is set, then you can represent 1095collating symbols inside lists. You form a @dfn{collating symbol} by 1096putting a collating element between an @dfn{open-collating-symbol 1097operator} and an @dfn{close-collating-symbol operator}. @samp{[.} 1098represents the open-collating-symbol operator and @samp{.]} represents 1099the close-collating-symbol operator. For example, if @samp{ll} is a 1100collating element, then @samp{[[.ll.]]} would match @samp{ll}. 1101 1102node Equivalence Class Operators 1103subsubsection Equivalence Class Operators (@code{[=} @dots{} @code{=]}) 1104@cindex equivalence class expression in regex 1105@cindex @samp{[=} in regex 1106@cindex @samp{=]} in regex 1107 1108If the syntax bit @code{XX} is set, then Regex recognizes equivalence class 1109expressions inside lists. A @dfn{equivalence class expression} is a set 1110of collating elements which all belong to the same equivalence class. 1111You form an equivalence class expression by putting a collating 1112element between an @dfn{open-equivalence-class operator} and a 1113@dfn{close-equivalence-class operator}. @samp{[=} represents the 1114open-equivalence-class operator and @samp{=]} represents the 1115close-equivalence-class operator. For example, if @samp{a} and @samp{A} 1116were an equivalence class, then both @samp{[[=a=]]} and @samp{[[=A=]]} 1117would match both @samp{a} and @samp{A}. If the collating element in an 1118equivalence class expression isn't part of an equivalence class, then 1119the matcher considers the equivalence class expression to be a collating 1120symbol. 1121 1122@end ignore 1123 1124@node Character Class Operators, Range Operator, , List Operators 1125@subsection Character Class Operators (@code{[:} @dots{} @code{:]}) 1126 1127@cindex character classes 1128@cindex @samp{[:} in regex 1129@cindex @samp{:]} in regex 1130 1131If the syntax bit @code{RE_CHARACTER_CLASSES} is set, then Regex 1132recognizes character class expressions inside lists. A @dfn{character 1133class expression} matches one character from a given class. You form a 1134character class expression by putting a character class name between an 1135@dfn{open-character-class operator} (represented by @samp{[:}) and a 1136@dfn{close-character-class operator} (represented by @samp{:]}). The 1137character class names and their meanings are: 1138 1139@table @code 1140 1141@item alnum 1142letters and digits 1143 1144@item alpha 1145letters 1146 1147@item blank 1148system-dependent; for @sc{gnu}, a space or tab 1149 1150@item cntrl 1151control characters (in the @sc{ascii} encoding, code 0177 and codes 1152less than 040) 1153 1154@item digit 1155digits 1156 1157@item graph 1158same as @code{print} except omits space 1159 1160@item lower 1161lowercase letters 1162 1163@item print 1164printable characters (in the @sc{ascii} encoding, space 1165tilde---codes 040 through 0176) 1166 1167@item punct 1168neither control nor alphanumeric characters 1169 1170@item space 1171space, carriage return, newline, vertical tab, and form feed 1172 1173@item upper 1174uppercase letters 1175 1176@item xdigit 1177hexadecimal digits: @code{0}--@code{9}, @code{a}--@code{f}, @code{A}--@code{F} 1178 1179@end table 1180 1181@noindent 1182These correspond to the definitions in the C library's @file{<ctype.h>} 1183facility. For example, @samp{[:alpha:]} corresponds to the standard 1184facility @code{isalpha}. Regex recognizes character class expressions 1185only inside of lists; so @samp{[[:alpha:]]} matches any letter, but 1186@samp{[:alpha:]} outside of a bracket expression and not followed by a 1187repetition operator matches just itself. 1188 1189@node Range Operator, , Character Class Operators, List Operators 1190@subsection The Range Operator (@code{-}) 1191 1192Regex recognizes @dfn{range expressions} inside a list. They represent 1193those characters 1194that fall between two elements in the current collating sequence. You 1195form a range expression by putting a @dfn{range operator} between two 1196@ignore 1197(If these get implemented, then substitute this for ``characters.'') 1198of any of the following: characters, collating elements, collating symbols, 1199and equivalence class expressions. The starting point of the range and 1200the ending point of the range don't have to be the same kind of item, 1201e.g., the starting point could be a collating element and the ending 1202point could be an equivalence class expression. If a range's ending 1203point is an equivalence class, then all the collating elements in that 1204class will be in the range. 1205@end ignore 1206characters.@footnote{You can't use a character class for the starting 1207or ending point of a range, since a character class is not a single 1208character.} @samp{-} represents the range operator. For example, 1209@samp{a-f} within a list represents all the characters from @samp{a} 1210through @samp{f} 1211inclusively. 1212 1213If the syntax bit @code{RE_NO_EMPTY_RANGES} is set, then if the range's 1214ending point collates less than its starting point, the range (and the 1215regular expression containing it) is invalid. For example, the regular 1216expression @samp{[z-a]} would be invalid. If this bit isn't set, then 1217Regex considers such a range to be empty. 1218 1219Since @samp{-} represents the range operator, if you want to make a 1220@samp{-} character itself 1221a list item, you must do one of the following: 1222 1223@itemize @bullet 1224@item 1225Put the @samp{-} either first or last in the list. 1226 1227@item 1228Include a range whose starting point collates strictly lower than 1229@samp{-} and whose ending point collates equal or higher. Unless a 1230range is the first item in a list, a @samp{-} can't be its starting 1231point, but @emph{can} be its ending point. That is because Regex 1232considers @samp{-} to be the range operator unless it is preceded by 1233another @samp{-}. For example, in the @sc{ascii} encoding, @samp{)}, 1234@samp{*}, @samp{+}, @samp{,}, @samp{-}, @samp{.}, and @samp{/} are 1235contiguous characters in the collating sequence. You might think that 1236@samp{[)-+--/]} has two ranges: @samp{)-+} and @samp{--/}. Rather, it 1237has the ranges @samp{)-+} and @samp{+--}, plus the character @samp{/}, so 1238it matches, e.g., @samp{,}, not @samp{.}. 1239 1240@item 1241Put a range whose starting point is @samp{-} first in the list. 1242 1243@end itemize 1244 1245For example, @samp{[-a-z]} matches a lowercase letter or a hyphen (in 1246English, in @sc{ascii}). 1247 1248 1249@node Grouping Operators, Back-reference Operator, List Operators, Common Operators 1250@section Grouping Operators (@code{(} @dots{} @code{)} or @code{\(} @dots{} @code{\)}) 1251 1252@kindex ( 1253@kindex ) 1254@kindex \( 1255@kindex \) 1256@cindex grouping 1257@cindex subexpressions 1258@cindex parenthesizing 1259 1260A @dfn{group}, also known as a @dfn{subexpression}, consists of an 1261@dfn{open-group operator}, any number of other operators, and a 1262@dfn{close-group operator}. Regex treats this sequence as a unit, just 1263as mathematics and programming languages treat a parenthesized 1264expression as a unit. 1265 1266Therefore, using @dfn{groups}, you can: 1267 1268@itemize @bullet 1269@item 1270delimit the argument(s) to an alternation operator (@pxref{Alternation 1271Operator}) or a repetition operator (@pxref{Repetition 1272Operators}). 1273 1274@item 1275keep track of the indices of the substring that matched a given group. 1276@xref{Using Registers}, for a precise explanation. 1277This lets you: 1278 1279@itemize @bullet 1280@item 1281use the back-reference operator (@pxref{Back-reference Operator}). 1282 1283@item 1284use registers (@pxref{Using Registers}). 1285 1286@end itemize 1287 1288@end itemize 1289 1290If the syntax bit @code{RE_NO_BK_PARENS} is set, then @samp{(} represents 1291the open-group operator and @samp{)} represents the 1292close-group operator; otherwise, @samp{\(} and @samp{\)} do. 1293 1294If the syntax bit @code{RE_UNMATCHED_RIGHT_PAREN_ORD} is set and a 1295close-group operator has no matching open-group operator, then Regex 1296considers it to match @samp{)}. 1297 1298 1299@node Back-reference Operator, Anchoring Operators, Grouping Operators, Common Operators 1300@section The Back-reference Operator (@dfn{\}@var{digit}) 1301 1302@cindex back references 1303 1304If the syntax bit @code{RE_NO_BK_REF} isn't set, then Regex recognizes 1305back references. A back reference matches a specified preceding group. 1306The back reference operator is represented by @samp{\@var{digit}} 1307anywhere after the end of a regular expression's @w{@var{digit}-th} 1308group (@pxref{Grouping Operators}). 1309 1310@var{digit} must be between @samp{1} and @samp{9}. The matcher assigns 1311numbers 1 through 9 to the first nine groups it encounters. By using 1312one of @samp{\1} through @samp{\9} after the corresponding group's 1313close-group operator, you can match a substring identical to the 1314one that the group does. 1315 1316Back references match according to the following (in all examples below, 1317@samp{(} represents the open-group, @samp{)} the close-group, @samp{@{} 1318the open-interval and @samp{@}} the close-interval operator): 1319 1320@itemize @bullet 1321@item 1322If the group matches a substring, the back reference matches an 1323identical substring. For example, @samp{(a)\1} matches @samp{aa} and 1324@samp{(bana)na\1bo\1} matches @samp{bananabanabobana}. Likewise, 1325@samp{(.*)\1} matches any (newline-free if the syntax bit 1326@code{RE_DOT_NEWLINE} isn't set) string that is composed of two 1327identical halves; the @samp{(.*)} matches the first half and the 1328@samp{\1} matches the second half. 1329 1330@item 1331If the group matches more than once (as it might if followed 1332by, e.g., a repetition operator), then the back reference matches the 1333substring the group @emph{last} matched. For example, 1334@samp{((a*)b)*\1\2} matches @samp{aabababa}; first @w{group 1} (the 1335outer one) matches @samp{aab} and @w{group 2} (the inner one) matches 1336@samp{aa}. Then @w{group 1} matches @samp{ab} and @w{group 2} matches 1337@samp{a}. So, @samp{\1} matches @samp{ab} and @samp{\2} matches 1338@samp{a}. 1339 1340@item 1341If the group doesn't participate in a match, i.e., it is part of an 1342alternative not taken or a repetition operator allows zero repetitions 1343of it, then the back reference makes the whole match fail. For example, 1344@samp{(one()|two())-and-(three\2|four\3)} matches @samp{one-and-three} 1345and @samp{two-and-four}, but not @samp{one-and-four} or 1346@samp{two-and-three}. For example, if the pattern matches 1347@samp{one-and-}, then its @w{group 2} matches the empty string and its 1348@w{group 3} doesn't participate in the match. So, if it then matches 1349@samp{four}, then when it tries to back reference @w{group 3}---which it 1350will attempt to do because @samp{\3} follows the @samp{four}---the match 1351will fail because @w{group 3} didn't participate in the match. 1352 1353@end itemize 1354 1355You can use a back reference as an argument to a repetition operator. For 1356example, @samp{(a(b))\2*} matches @samp{a} followed by two or more 1357@samp{b}s. Similarly, @samp{(a(b))\2@{3@}} matches @samp{abbbb}. 1358 1359If there is no preceding @w{@var{digit}-th} subexpression, the regular 1360expression is invalid. 1361 1362 1363@node Anchoring Operators, , Back-reference Operator, Common Operators 1364@section Anchoring Operators 1365 1366@cindex anchoring 1367@cindex regexp anchoring 1368 1369These operators can constrain a pattern to match only at the beginning or 1370end of the entire string or at the beginning or end of a line. 1371 1372@menu 1373* Match-beginning-of-line Operator:: ^ 1374* Match-end-of-line Operator:: $ 1375@end menu 1376 1377 1378@node Match-beginning-of-line Operator, Match-end-of-line Operator, , Anchoring Operators 1379@subsection The Match-beginning-of-line Operator (@code{^}) 1380 1381@kindex ^ 1382@cindex beginning-of-line operator 1383@cindex anchors 1384 1385This operator can match the empty string either at the beginning of the 1386string or after a newline character. Thus, it is said to @dfn{anchor} 1387the pattern to the beginning of a line. 1388 1389In the cases following, @samp{^} represents this operator. (Otherwise, 1390@samp{^} is ordinary.) 1391 1392@itemize @bullet 1393 1394@item 1395It (the @samp{^}) is first in the pattern, as in @samp{^foo}. 1396 1397@cnindex RE_CONTEXT_INDEP_ANCHORS @r{(and @samp{^})} 1398@item 1399The syntax bit @code{RE_CONTEXT_INDEP_ANCHORS} is set, and it is outside 1400a bracket expression. 1401 1402@cindex open-group operator and @samp{^} 1403@cindex alternation operator and @samp{^} 1404@item 1405It follows an open-group or alternation operator, as in @samp{a\(^b\)} 1406and @samp{a\|^b}. @xref{Grouping Operators}, and @ref{Alternation 1407Operator}. 1408 1409@end itemize 1410 1411These rules imply that some valid patterns containing @samp{^} cannot be 1412matched; for example, @samp{foo^bar} if @code{RE_CONTEXT_INDEP_ANCHORS} 1413is set. 1414 1415@vindex not_bol @r{field in pattern buffer} 1416If the @code{not_bol} field is set in the pattern buffer (@pxref{GNU 1417Pattern Buffers}), then @samp{^} fails to match at the beginning of the 1418string. @xref{POSIX Matching}, for when you might find this useful. 1419 1420@vindex newline_anchor @r{field in pattern buffer} 1421If the @code{newline_anchor} field is set in the pattern buffer, then 1422@samp{^} fails to match after a newline. This is useful when you do not 1423regard the string to be matched as broken into lines. 1424 1425 1426@node Match-end-of-line Operator, , Match-beginning-of-line Operator, Anchoring Operators 1427@subsection The Match-end-of-line Operator (@code{$}) 1428 1429@kindex $ 1430@cindex end-of-line operator 1431@cindex anchors 1432 1433This operator can match the empty string either at the end of 1434the string or before a newline character in the string. Thus, it is 1435said to @dfn{anchor} the pattern to the end of a line. 1436 1437It is always represented by @samp{$}. For example, @samp{foo$} usually 1438matches, e.g., @samp{foo} and, e.g., the first three characters of 1439@samp{foo\nbar}. 1440 1441Its interaction with the syntax bits and pattern buffer fields is 1442exactly the dual of @samp{^}'s; see the previous section. (That is, 1443``beginning'' becomes ``end'', ``next'' becomes ``previous'', and 1444``after'' becomes ``before''.) 1445 1446 1447@node GNU Operators, GNU Emacs Operators, Common Operators, Top 1448@chapter GNU Operators 1449 1450Following are operators that @sc{gnu} defines (and @sc{posix} doesn't). 1451 1452@menu 1453* Word Operators:: 1454* Buffer Operators:: 1455@end menu 1456 1457@node Word Operators, Buffer Operators, , GNU Operators 1458@section Word Operators 1459 1460The operators in this section require Regex to recognize parts of words. 1461Regex uses a syntax table to determine whether or not a character is 1462part of a word, i.e., whether or not it is @dfn{word-constituent}. 1463 1464@menu 1465* Non-Emacs Syntax Tables:: 1466* Match-word-boundary Operator:: \b 1467* Match-within-word Operator:: \B 1468* Match-beginning-of-word Operator:: \< 1469* Match-end-of-word Operator:: \> 1470* Match-word-constituent Operator:: \w 1471* Match-non-word-constituent Operator:: \W 1472@end menu 1473 1474@node Non-Emacs Syntax Tables, Match-word-boundary Operator, , Word Operators 1475@subsection Non-Emacs Syntax Tables 1476 1477A @dfn{syntax table} is an array indexed by the characters in your 1478character set. In the @sc{ascii} encoding, therefore, a syntax table 1479has 256 elements. Regex always uses a @code{char *} variable 1480@code{re_syntax_table} as its syntax table. In some cases, it 1481initializes this variable and in others it expects you to initialize it. 1482 1483@itemize @bullet 1484@item 1485If Regex is compiled with the preprocessor symbols @code{emacs} and 1486@code{SYNTAX_TABLE} both undefined, then Regex allocates 1487@code{re_syntax_table} and initializes an element @var{i} either to 1488@code{Sword} (which it defines) if @var{i} is a letter, number, or 1489@samp{_}, or to zero if it's not. 1490 1491@item 1492If Regex is compiled with @code{emacs} undefined but @code{SYNTAX_TABLE} 1493defined, then Regex expects you to define a @code{char *} variable 1494@code{re_syntax_table} to be a valid syntax table. 1495 1496@item 1497@xref{Emacs Syntax Tables}, for what happens when Regex is compiled with 1498the preprocessor symbol @code{emacs} defined. 1499 1500@end itemize 1501 1502@node Match-word-boundary Operator, Match-within-word Operator, Non-Emacs Syntax Tables, Word Operators 1503@subsection The Match-word-boundary Operator (@code{\b}) 1504 1505@cindex @samp{\b} 1506@cindex word boundaries, matching 1507 1508This operator (represented by @samp{\b}) matches the empty string at 1509either the beginning or the end of a word. For example, @samp{\brat\b} 1510matches the separate word @samp{rat}. 1511 1512@node Match-within-word Operator, Match-beginning-of-word Operator, Match-word-boundary Operator, Word Operators 1513@subsection The Match-within-word Operator (@code{\B}) 1514 1515@cindex @samp{\B} 1516 1517This operator (represented by @samp{\B}) matches the empty string within 1518a word. For example, @samp{c\Brat\Be} matches @samp{crate}, but 1519@samp{dirty \Brat} doesn't match @samp{dirty rat}. 1520 1521@node Match-beginning-of-word Operator, Match-end-of-word Operator, Match-within-word Operator, Word Operators 1522@subsection The Match-beginning-of-word Operator (@code{\<}) 1523 1524@cindex @samp{\<} 1525 1526This operator (represented by @samp{\<}) matches the empty string at the 1527beginning of a word. 1528 1529@node Match-end-of-word Operator, Match-word-constituent Operator, Match-beginning-of-word Operator, Word Operators 1530@subsection The Match-end-of-word Operator (@code{\>}) 1531 1532@cindex @samp{\>} 1533 1534This operator (represented by @samp{\>}) matches the empty string at the 1535end of a word. 1536 1537@node Match-word-constituent Operator, Match-non-word-constituent Operator, Match-end-of-word Operator, Word Operators 1538@subsection The Match-word-constituent Operator (@code{\w}) 1539 1540@cindex @samp{\w} 1541 1542This operator (represented by @samp{\w}) matches any word-constituent 1543character. 1544 1545@node Match-non-word-constituent Operator, , Match-word-constituent Operator, Word Operators 1546@subsection The Match-non-word-constituent Operator (@code{\W}) 1547 1548@cindex @samp{\W} 1549 1550This operator (represented by @samp{\W}) matches any character that is 1551not word-constituent. 1552 1553 1554@node Buffer Operators, , Word Operators, GNU Operators 1555@section Buffer Operators 1556 1557Following are operators which work on buffers. In Emacs, a @dfn{buffer} 1558is, naturally, an Emacs buffer. For other programs, Regex considers the 1559entire string to be matched as the buffer. 1560 1561@menu 1562* Match-beginning-of-buffer Operator:: \` 1563* Match-end-of-buffer Operator:: \' 1564@end menu 1565 1566 1567@node Match-beginning-of-buffer Operator, Match-end-of-buffer Operator, , Buffer Operators 1568@subsection The Match-beginning-of-buffer Operator 1569@c (@code{\`}) 1570 1571@cindex @samp{\`} 1572 1573This operator (represented by @samp{\`}) matches the empty string at the 1574beginning of the buffer. 1575 1576@node Match-end-of-buffer Operator, , Match-beginning-of-buffer Operator, Buffer Operators 1577@subsection The Match-end-of-buffer Operator 1578@c (@code{\'}) 1579 1580@cindex @samp{\'} 1581 1582This operator (represented by @samp{\'}) matches the empty string at the 1583end of the buffer. 1584 1585 1586@node GNU Emacs Operators, What Gets Matched?, GNU Operators, Top 1587@chapter GNU Emacs Operators 1588 1589Following are operators that @sc{gnu} defines (and @sc{posix} doesn't) 1590that you can use only when Regex is compiled with the preprocessor 1591symbol @code{emacs} defined. 1592 1593@menu 1594* Syntactic Class Operators:: 1595@end menu 1596 1597 1598@node Syntactic Class Operators, , , GNU Emacs Operators 1599@section Syntactic Class Operators 1600 1601The operators in this section require Regex to recognize the syntactic 1602classes of characters. Regex uses a syntax table to determine this. 1603 1604@menu 1605* Emacs Syntax Tables:: 1606* Match-syntactic-class Operator:: \sCLASS 1607* Match-not-syntactic-class Operator:: \SCLASS 1608@end menu 1609 1610@node Emacs Syntax Tables, Match-syntactic-class Operator, , Syntactic Class Operators 1611@subsection Emacs Syntax Tables 1612 1613A @dfn{syntax table} is an array indexed by the characters in your 1614character set. In the @sc{ascii} encoding, therefore, a syntax table 1615has 256 elements. 1616 1617If Regex is compiled with the preprocessor symbol @code{emacs} defined, 1618then Regex expects you to define and initialize the variable 1619@code{re_syntax_table} to be an Emacs syntax table. Emacs' syntax 1620tables are more complicated than Regex's own (@pxref{Non-Emacs Syntax 1621Tables}). @xref{Syntax, , Syntax, emacs, The GNU Emacs User's Manual}, 1622for a description of Emacs' syntax tables. 1623 1624@node Match-syntactic-class Operator, Match-not-syntactic-class Operator, Emacs Syntax Tables, Syntactic Class Operators 1625@subsection The Match-syntactic-class Operator (@code{\s}@var{class}) 1626 1627@cindex @samp{\s} 1628 1629This operator matches any character whose syntactic class is represented 1630by a specified character. @samp{\s@var{class}} represents this operator 1631where @var{class} is the character representing the syntactic class you 1632want. For example, @samp{w} represents the syntactic 1633class of word-constituent characters, so @samp{\sw} matches any 1634word-constituent character. 1635 1636@node Match-not-syntactic-class Operator, , Match-syntactic-class Operator, Syntactic Class Operators 1637@subsection The Match-not-syntactic-class Operator (@code{\S}@var{class}) 1638 1639@cindex @samp{\S} 1640 1641This operator is similar to the match-syntactic-class operator except 1642that it matches any character whose syntactic class is @emph{not} 1643represented by the specified character. @samp{\S@var{class}} represents 1644this operator. For example, @samp{w} represents the syntactic class of 1645word-constituent characters, so @samp{\Sw} matches any character that is 1646not word-constituent. 1647 1648 1649@node What Gets Matched?, Programming with Regex, GNU Emacs Operators, Top 1650@chapter What Gets Matched? 1651 1652Regex usually matches strings according to the ``leftmost longest'' 1653rule; that is, it chooses the longest of the leftmost matches. This 1654does not mean that for a regular expression containing subexpressions 1655that it simply chooses the longest match for each subexpression, left to 1656right; the overall match must also be the longest possible one. 1657 1658For example, @samp{(ac*)(c*d[ac]*)\1} matches @samp{acdacaaa}, not 1659@samp{acdac}, as it would if it were to choose the longest match for the 1660first subexpression. 1661 1662 1663@node Programming with Regex, Copying, What Gets Matched?, Top 1664@chapter Programming with Regex 1665 1666Here we describe how you use the Regex data structures and functions in 1667C programs. Regex has three interfaces: one designed for @sc{gnu}, one 1668compatible with @sc{posix} and one compatible with Berkeley @sc{unix}. 1669 1670@menu 1671* GNU Regex Functions:: 1672* POSIX Regex Functions:: 1673* BSD Regex Functions:: 1674@end menu 1675 1676 1677@node GNU Regex Functions, POSIX Regex Functions, , Programming with Regex 1678@section GNU Regex Functions 1679 1680If you're writing code that doesn't need to be compatible with either 1681@sc{posix} or Berkeley @sc{unix}, you can use these functions. They 1682provide more options than the other interfaces. 1683 1684@menu 1685* GNU Pattern Buffers:: The re_pattern_buffer type. 1686* GNU Regular Expression Compiling:: re_compile_pattern () 1687* GNU Matching:: re_match () 1688* GNU Searching:: re_search () 1689* Matching/Searching with Split Data:: re_match_2 (), re_search_2 () 1690* Searching with Fastmaps:: re_compile_fastmap () 1691* GNU Translate Tables:: The `translate' field. 1692* Using Registers:: The re_registers type and related fns. 1693* Freeing GNU Pattern Buffers:: regfree () 1694@end menu 1695 1696 1697@node GNU Pattern Buffers, GNU Regular Expression Compiling, , GNU Regex Functions 1698@subsection GNU Pattern Buffers 1699 1700@cindex pattern buffer, definition of 1701@tindex re_pattern_buffer @r{definition} 1702@tindex struct re_pattern_buffer @r{definition} 1703 1704To compile, match, or search for a given regular expression, you must 1705supply a pattern buffer. A @dfn{pattern buffer} holds one compiled 1706regular expression.@footnote{Regular expressions are also referred to as 1707``patterns,'' hence the name ``pattern buffer.''} 1708 1709You can have several different pattern buffers simultaneously, each 1710holding a compiled pattern for a different regular expression. 1711 1712@file{regex.h} defines the pattern buffer @code{struct} as follows: 1713 1714@example 1715 /* Space that holds the compiled pattern. It is declared as 1716 `unsigned char *' because its elements are 1717 sometimes used as array indexes. */ 1718 unsigned char *buffer; 1719 1720 /* Number of bytes to which `buffer' points. */ 1721 unsigned long allocated; 1722 1723 /* Number of bytes actually used in `buffer'. */ 1724 unsigned long used; 1725 1726 /* Syntax setting with which the pattern was compiled. */ 1727 reg_syntax_t syntax; 1728 1729 /* Pointer to a fastmap, if any, otherwise zero. re_search uses 1730 the fastmap, if there is one, to skip over impossible 1731 starting points for matches. */ 1732 char *fastmap; 1733 1734 /* Either a translate table to apply to all characters before 1735 comparing them, or zero for no translation. The translation 1736 is applied to a pattern when it is compiled and to a string 1737 when it is matched. */ 1738 char *translate; 1739 1740 /* Number of subexpressions found by the compiler. */ 1741 size_t re_nsub; 1742 1743 /* Zero if this pattern cannot match the empty string, one else. 1744 Well, in truth it's used only in `re_search_2', to see 1745 whether or not we should use the fastmap, so we don't set 1746 this absolutely perfectly; see `re_compile_fastmap' (the 1747 `duplicate' case). */ 1748 unsigned can_be_null : 1; 1749 1750 /* If REGS_UNALLOCATED, allocate space in the `regs' structure 1751 for `max (RE_NREGS, re_nsub + 1)' groups. 1752 If REGS_REALLOCATE, reallocate space if necessary. 1753 If REGS_FIXED, use what's there. */ 1754#define REGS_UNALLOCATED 0 1755#define REGS_REALLOCATE 1 1756#define REGS_FIXED 2 1757 unsigned regs_allocated : 2; 1758 1759 /* Set to zero when `regex_compile' compiles a pattern; set to one 1760 by `re_compile_fastmap' if it updates the fastmap. */ 1761 unsigned fastmap_accurate : 1; 1762 1763 /* If set, `re_match_2' does not return information about 1764 subexpressions. */ 1765 unsigned no_sub : 1; 1766 1767 /* If set, a beginning-of-line anchor doesn't match at the 1768 beginning of the string. */ 1769 unsigned not_bol : 1; 1770 1771 /* Similarly for an end-of-line anchor. */ 1772 unsigned not_eol : 1; 1773 1774 /* If true, an anchor at a newline matches. */ 1775 unsigned newline_anchor : 1; 1776 1777@end example 1778 1779 1780@node GNU Regular Expression Compiling, GNU Matching, GNU Pattern Buffers, GNU Regex Functions 1781@subsection GNU Regular Expression Compiling 1782 1783In @sc{gnu}, you can both match and search for a given regular 1784expression. To do either, you must first compile it in a pattern buffer 1785(@pxref{GNU Pattern Buffers}). 1786 1787@cindex syntax initialization 1788@vindex re_syntax_options @r{initialization} 1789Regular expressions match according to the syntax with which they were 1790compiled; with @sc{gnu}, you indicate what syntax you want by setting 1791the variable @code{re_syntax_options} (declared in @file{regex.h} and 1792defined in @file{regex.c}) before calling the compiling function, 1793@code{re_compile_pattern} (see below). @xref{Syntax Bits}, and 1794@ref{Predefined Syntaxes}. 1795 1796You can change the value of @code{re_syntax_options} at any time. 1797Usually, however, you set its value once and then never change it. 1798 1799@cindex pattern buffer initialization 1800@code{re_compile_pattern} takes a pattern buffer as an argument. You 1801must initialize the following fields: 1802 1803@table @code 1804 1805@item translate @r{initialization} 1806 1807@item translate 1808@vindex translate @r{initialization} 1809Initialize this to point to a translate table if you want one, or to 1810zero if you don't. We explain translate tables in @ref{GNU Translate 1811Tables}. 1812 1813@item fastmap 1814@vindex fastmap @r{initialization} 1815Initialize this to nonzero if you want a fastmap, or to zero if you 1816don't. 1817 1818@item buffer 1819@itemx allocated 1820@vindex buffer @r{initialization} 1821@vindex allocated @r{initialization} 1822@findex malloc 1823If you want @code{re_compile_pattern} to allocate memory for the 1824compiled pattern, set both of these to zero. If you have an existing 1825block of memory (allocated with @code{malloc}) you want Regex to use, 1826set @code{buffer} to its address and @code{allocated} to its size (in 1827bytes). 1828 1829@code{re_compile_pattern} uses @code{realloc} to extend the space for 1830the compiled pattern as necessary. 1831 1832@end table 1833 1834To compile a pattern buffer, use: 1835 1836@findex re_compile_pattern 1837@example 1838char * 1839re_compile_pattern (const char *@var{regex}, const int @var{regex_size}, 1840 struct re_pattern_buffer *@var{pattern_buffer}) 1841@end example 1842 1843@noindent 1844@var{regex} is the regular expression's address, @var{regex_size} is its 1845length, and @var{pattern_buffer} is the pattern buffer's address. 1846 1847If @code{re_compile_pattern} successfully compiles the regular 1848expression, it returns zero and sets @code{*@var{pattern_buffer}} to the 1849compiled pattern. It sets the pattern buffer's fields as follows: 1850 1851@table @code 1852@item buffer 1853@vindex buffer @r{field, set by @code{re_compile_pattern}} 1854to the compiled pattern. 1855 1856@item used 1857@vindex used @r{field, set by @code{re_compile_pattern}} 1858to the number of bytes the compiled pattern in @code{buffer} occupies. 1859 1860@item syntax 1861@vindex syntax @r{field, set by @code{re_compile_pattern}} 1862to the current value of @code{re_syntax_options}. 1863 1864@item re_nsub 1865@vindex re_nsub @r{field, set by @code{re_compile_pattern}} 1866to the number of subexpressions in @var{regex}. 1867 1868@item fastmap_accurate 1869@vindex fastmap_accurate @r{field, set by @code{re_compile_pattern}} 1870to zero on the theory that the pattern you're compiling is different 1871than the one previously compiled into @code{buffer}; in that case (since 1872you can't make a fastmap without a compiled pattern), 1873@code{fastmap} would either contain an incompatible fastmap, or nothing 1874at all. 1875 1876@c xx what else? 1877@end table 1878 1879If @code{re_compile_pattern} can't compile @var{regex}, it returns an 1880error string corresponding to one of the errors listed in @ref{POSIX 1881Regular Expression Compiling}. 1882 1883 1884@node GNU Matching, GNU Searching, GNU Regular Expression Compiling, GNU Regex Functions 1885@subsection GNU Matching 1886 1887@cindex matching with GNU functions 1888 1889Matching the @sc{gnu} way means trying to match as much of a string as 1890possible starting at a position within it you specify. Once you've compiled 1891a pattern into a pattern buffer (@pxref{GNU Regular Expression 1892Compiling}), you can ask the matcher to match that pattern against a 1893string using: 1894 1895@findex re_match 1896@example 1897int 1898re_match (struct re_pattern_buffer *@var{pattern_buffer}, 1899 const char *@var{string}, const int @var{size}, 1900 const int @var{start}, struct re_registers *@var{regs}) 1901@end example 1902 1903@noindent 1904@var{pattern_buffer} is the address of a pattern buffer containing a 1905compiled pattern. @var{string} is the string you want to match; it can 1906contain newline and null characters. @var{size} is the length of that 1907string. @var{start} is the string index at which you want to 1908begin matching; the first character of @var{string} is at index zero. 1909@xref{Using Registers}, for a explanation of @var{regs}; you can safely 1910pass zero. 1911 1912@code{re_match} matches the regular expression in @var{pattern_buffer} 1913against the string @var{string} according to the syntax in 1914@var{pattern_buffers}'s @code{syntax} field. (@xref{GNU Regular 1915Expression Compiling}, for how to set it.) The function returns 1916@math{-1} if the compiled pattern does not match any part of 1917@var{string} and @math{-2} if an internal error happens; otherwise, it 1918returns how many (possibly zero) characters of @var{string} the pattern 1919matched. 1920 1921An example: suppose @var{pattern_buffer} points to a pattern buffer 1922containing the compiled pattern for @samp{a*}, and @var{string} points 1923to @samp{aaaaab} (whereupon @var{size} should be 6). Then if @var{start} 1924is 2, @code{re_match} returns 3, i.e., @samp{a*} would have matched the 1925last three @samp{a}s in @var{string}. If @var{start} is 0, 1926@code{re_match} returns 5, i.e., @samp{a*} would have matched all the 1927@samp{a}s in @var{string}. If @var{start} is either 5 or 6, it returns 1928zero. 1929 1930If @var{start} is not between zero and @var{size}, then 1931@code{re_match} returns @math{-1}. 1932 1933 1934@node GNU Searching, Matching/Searching with Split Data, GNU Matching, GNU Regex Functions 1935@subsection GNU Searching 1936 1937@cindex searching with GNU functions 1938 1939@dfn{Searching} means trying to match starting at successive positions 1940within a string. The function @code{re_search} does this. 1941 1942Before calling @code{re_search}, you must compile your regular 1943expression. @xref{GNU Regular Expression Compiling}. 1944 1945Here is the function declaration: 1946 1947@findex re_search 1948@example 1949int 1950re_search (struct re_pattern_buffer *@var{pattern_buffer}, 1951 const char *@var{string}, const int @var{size}, 1952 const int @var{start}, const int @var{range}, 1953 struct re_registers *@var{regs}) 1954@end example 1955 1956@noindent 1957@vindex start @r{argument to @code{re_search}} 1958@vindex range @r{argument to @code{re_search}} 1959whose arguments are the same as those to @code{re_match} (@pxref{GNU 1960Matching}) except that the two arguments @var{start} and @var{range} 1961replace @code{re_match}'s argument @var{start}. 1962 1963If @var{range} is positive, then @code{re_search} attempts a match 1964starting first at index @var{start}, then at @math{@var{start} + 1} if 1965that fails, and so on, up to @math{@var{start} + @var{range}}; if 1966@var{range} is negative, then it attempts a match starting first at 1967index @var{start}, then at @math{@var{start} -1} if that fails, and so 1968on. 1969 1970If @var{start} is not between zero and @var{size}, then @code{re_search} 1971returns @math{-1}. When @var{range} is positive, @code{re_search} 1972adjusts @var{range} so that @math{@var{start} + @var{range} - 1} is 1973between zero and @var{size}, if necessary; that way it won't search 1974outside of @var{string}. Similarly, when @var{range} is negative, 1975@code{re_search} adjusts @var{range} so that @math{@var{start} + 1976@var{range} + 1} is between zero and @var{size}, if necessary. 1977 1978If the @code{fastmap} field of @var{pattern_buffer} is zero, 1979@code{re_search} matches starting at consecutive positions; otherwise, 1980it uses @code{fastmap} to make the search more efficient. 1981@xref{Searching with Fastmaps}. 1982 1983If no match is found, @code{re_search} returns @math{-1}. If 1984a match is found, it returns the index where the match began. If an 1985internal error happens, it returns @math{-2}. 1986 1987 1988@node Matching/Searching with Split Data, Searching with Fastmaps, GNU Searching, GNU Regex Functions 1989@subsection Matching and Searching with Split Data 1990 1991Using the functions @code{re_match_2} and @code{re_search_2}, you can 1992match or search in data that is divided into two strings. 1993 1994The function: 1995 1996@findex re_match_2 1997@example 1998int 1999re_match_2 (struct re_pattern_buffer *@var{buffer}, 2000 const char *@var{string1}, const int @var{size1}, 2001 const char *@var{string2}, const int @var{size2}, 2002 const int @var{start}, 2003 struct re_registers *@var{regs}, 2004 const int @var{stop}) 2005@end example 2006 2007@noindent 2008is similar to @code{re_match} (@pxref{GNU Matching}) except that you 2009pass @emph{two} data strings and sizes, and an index @var{stop} beyond 2010which you don't want the matcher to try matching. As with 2011@code{re_match}, if it succeeds, @code{re_match_2} returns how many 2012characters of @var{string} it matched. Regard @var{string1} and 2013@var{string2} as concatenated when you set the arguments @var{start} and 2014@var{stop} and use the contents of @var{regs}; @code{re_match_2} never 2015returns a value larger than @math{@var{size1} + @var{size2}}. 2016 2017The function: 2018 2019@findex re_search_2 2020@example 2021int 2022re_search_2 (struct re_pattern_buffer *@var{buffer}, 2023 const char *@var{string1}, const int @var{size1}, 2024 const char *@var{string2}, const int @var{size2}, 2025 const int @var{start}, const int @var{range}, 2026 struct re_registers *@var{regs}, 2027 const int @var{stop}) 2028@end example 2029 2030@noindent 2031is similarly related to @code{re_search}. 2032 2033 2034@node Searching with Fastmaps, GNU Translate Tables, Matching/Searching with Split Data, GNU Regex Functions 2035@subsection Searching with Fastmaps 2036 2037@cindex fastmaps 2038If you're searching through a long string, you should use a fastmap. 2039Without one, the searcher tries to match at consecutive positions in the 2040string. Generally, most of the characters in the string could not start 2041a match. It takes much longer to try matching at a given position in the 2042string than it does to check in a table whether or not the character at 2043that position could start a match. A @dfn{fastmap} is such a table. 2044 2045More specifically, a fastmap is an array indexed by the characters in 2046your character set. Under the @sc{ascii} encoding, therefore, a fastmap 2047has 256 elements. If you want the searcher to use a fastmap with a 2048given pattern buffer, you must allocate the array and assign the array's 2049address to the pattern buffer's @code{fastmap} field. You either can 2050compile the fastmap yourself or have @code{re_search} do it for you; 2051when @code{fastmap} is nonzero, it automatically compiles a fastmap the 2052first time you search using a particular compiled pattern. 2053 2054To compile a fastmap yourself, use: 2055 2056@findex re_compile_fastmap 2057@example 2058int 2059re_compile_fastmap (struct re_pattern_buffer *@var{pattern_buffer}) 2060@end example 2061 2062@noindent 2063@var{pattern_buffer} is the address of a pattern buffer. If the 2064character @var{c} could start a match for the pattern, 2065@code{re_compile_fastmap} makes 2066@code{@var{pattern_buffer}->fastmap[@var{c}]} nonzero. It returns 2067@math{0} if it can compile a fastmap and @math{-2} if there is an 2068internal error. For example, if @samp{|} is the alternation operator 2069and @var{pattern_buffer} holds the compiled pattern for @samp{a|b}, then 2070@code{re_compile_fastmap} sets @code{fastmap['a']} and 2071@code{fastmap['b']} (and no others). 2072 2073@code{re_search} uses a fastmap as it moves along in the string: it 2074checks the string's characters until it finds one that's in the fastmap. 2075Then it tries matching at that character. If the match fails, it 2076repeats the process. So, by using a fastmap, @code{re_search} doesn't 2077waste time trying to match at positions in the string that couldn't 2078start a match. 2079 2080If you don't want @code{re_search} to use a fastmap, 2081store zero in the @code{fastmap} field of the pattern buffer before 2082calling @code{re_search}. 2083 2084Once you've initialized a pattern buffer's @code{fastmap} field, you 2085need never do so again---even if you compile a new pattern in 2086it---provided the way the field is set still reflects whether or not you 2087want a fastmap. @code{re_search} will still either do nothing if 2088@code{fastmap} is null or, if it isn't, compile a new fastmap for the 2089new pattern. 2090 2091@node GNU Translate Tables, Using Registers, Searching with Fastmaps, GNU Regex Functions 2092@subsection GNU Translate Tables 2093 2094If you set the @code{translate} field of a pattern buffer to a translate 2095table, then the @sc{gnu} Regex functions to which you've passed that 2096pattern buffer use it to apply a simple transformation 2097to all the regular expression and string characters at which they look. 2098 2099A @dfn{translate table} is an array indexed by the characters in your 2100character set. Under the @sc{ascii} encoding, therefore, a translate 2101table has 256 elements. The array's elements are also characters in 2102your character set. When the Regex functions see a character @var{c}, 2103they use @code{translate[@var{c}]} in its place, with one exception: the 2104character after a @samp{\} is not translated. (This ensures that, the 2105operators, e.g., @samp{\B} and @samp{\b}, are always distinguishable.) 2106 2107For example, a table that maps all lowercase letters to the 2108corresponding uppercase ones would cause the matcher to ignore 2109differences in case.@footnote{A table that maps all uppercase letters to 2110the corresponding lowercase ones would work just as well for this 2111purpose.} Such a table would map all characters except lowercase letters 2112to themselves, and lowercase letters to the corresponding uppercase 2113ones. Under the @sc{ascii} encoding, here's how you could initialize 2114such a table (we'll call it @code{case_fold}): 2115 2116@example 2117for (i = 0; i < 256; i++) 2118 case_fold[i] = i; 2119for (i = 'a'; i <= 'z'; i++) 2120 case_fold[i] = i - ('a' - 'A'); 2121@end example 2122 2123You tell Regex to use a translate table on a given pattern buffer by 2124assigning that table's address to the @code{translate} field of that 2125buffer. If you don't want Regex to do any translation, put zero into 2126this field. You'll get weird results if you change the table's contents 2127anytime between compiling the pattern buffer, compiling its fastmap, and 2128matching or searching with the pattern buffer. 2129 2130@node Using Registers, Freeing GNU Pattern Buffers, GNU Translate Tables, GNU Regex Functions 2131@subsection Using Registers 2132 2133A group in a regular expression can match a (posssibly empty) substring 2134of the string that regular expression as a whole matched. The matcher 2135remembers the beginning and end of the substring matched by 2136each group. 2137 2138To find out what they matched, pass a nonzero @var{regs} argument to a 2139@sc{gnu} matching or searching function (@pxref{GNU Matching} and 2140@ref{GNU Searching}), i.e., the address of a structure of this type, as 2141defined in @file{regex.h}: 2142 2143@c We don't bother to include this directly from regex.h, 2144@c since it changes so rarely. 2145@example 2146@tindex re_registers 2147@vindex num_regs @r{in @code{struct re_registers}} 2148@vindex start @r{in @code{struct re_registers}} 2149@vindex end @r{in @code{struct re_registers}} 2150struct re_registers 2151@{ 2152 unsigned num_regs; 2153 regoff_t *start; 2154 regoff_t *end; 2155@}; 2156@end example 2157 2158Except for (possibly) the @var{num_regs}'th element (see below), the 2159@var{i}th element of the @code{start} and @code{end} arrays records 2160information about the @var{i}th group in the pattern. (They're declared 2161as C pointers, but this is only because not all C compilers accept 2162zero-length arrays; conceptually, it is simplest to think of them as 2163arrays.) 2164 2165The @code{start} and @code{end} arrays are allocated in various ways, 2166depending on the value of the @code{regs_allocated} 2167@vindex regs_allocated 2168field in the pattern buffer passed to the matcher. 2169 2170The simplest and perhaps most useful is to let the matcher (re)allocate 2171enough space to record information for all the groups in the regular 2172expression. If @code{regs_allocated} is @code{REGS_UNALLOCATED}, 2173@vindex REGS_UNALLOCATED 2174the matcher allocates @math{1 + @var{re_nsub}} (another field in the 2175pattern buffer; @pxref{GNU Pattern Buffers}). The extra element is set 2176to @math{-1}, and sets @code{regs_allocated} to @code{REGS_REALLOCATE}. 2177@vindex REGS_REALLOCATE 2178Then on subsequent calls with the same pattern buffer and @var{regs} 2179arguments, the matcher reallocates more space if necessary. 2180 2181It would perhaps be more logical to make the @code{regs_allocated} field 2182part of the @code{re_registers} structure, instead of part of the 2183pattern buffer. But in that case the caller would be forced to 2184initialize the structure before passing it. Much existing code doesn't 2185do this initialization, and it's arguably better to avoid it anyway. 2186 2187@code{re_compile_pattern} sets @code{regs_allocated} to 2188@code{REGS_UNALLOCATED}, 2189so if you use the GNU regular expression 2190functions, you get this behavior by default. 2191 2192xx document re_set_registers 2193 2194@sc{posix}, on the other hand, requires a different interface: the 2195caller is supposed to pass in a fixed-length array which the matcher 2196fills. Therefore, if @code{regs_allocated} is @code{REGS_FIXED} 2197@vindex REGS_FIXED 2198the matcher simply fills that array. 2199 2200The following examples illustrate the information recorded in the 2201@code{re_registers} structure. (In all of them, @samp{(} represents the 2202open-group and @samp{)} the close-group operator. The first character 2203in the string @var{string} is at index 0.) 2204 2205@c xx i'm not sure this is all true anymore. 2206 2207@itemize @bullet 2208 2209@item 2210If the regular expression has an @w{@var{i}-th} 2211group not contained within another group that matches a 2212substring of @var{string}, then the function sets 2213@code{@w{@var{regs}->}start[@var{i}]} to the index in @var{string} where 2214the substring matched by the @w{@var{i}-th} group begins, and 2215@code{@w{@var{regs}->}end[@var{i}]} to the index just beyond that 2216substring's end. The function sets @code{@w{@var{regs}->}start[0]} and 2217@code{@w{@var{regs}->}end[0]} to analogous information about the entire 2218pattern. 2219 2220For example, when you match @samp{((a)(b))} against @samp{ab}, you get: 2221 2222@itemize @minus 2223@item 22240 in @code{@w{@var{regs}->}start[0]} and 2 in @code{@w{@var{regs}->}end[0]} 2225 2226@item 22270 in @code{@w{@var{regs}->}start[1]} and 2 in @code{@w{@var{regs}->}end[1]} 2228 2229@item 22300 in @code{@w{@var{regs}->}start[2]} and 1 in @code{@w{@var{regs}->}end[2]} 2231 2232@item 22331 in @code{@w{@var{regs}->}start[3]} and 2 in @code{@w{@var{regs}->}end[3]} 2234@end itemize 2235 2236@item 2237If a group matches more than once (as it might if followed by, 2238e.g., a repetition operator), then the function reports the information 2239about what the group @emph{last} matched. 2240 2241For example, when you match the pattern @samp{(a)*} against the string 2242@samp{aa}, you get: 2243 2244@itemize @minus 2245@item 22460 in @code{@w{@var{regs}->}start[0]} and 2 in @code{@w{@var{regs}->}end[0]} 2247 2248@item 22491 in @code{@w{@var{regs}->}start[1]} and 2 in @code{@w{@var{regs}->}end[1]} 2250@end itemize 2251 2252@item 2253If the @w{@var{i}-th} group does not participate in a 2254successful match, e.g., it is an alternative not taken or a 2255repetition operator allows zero repetitions of it, then the function 2256sets @code{@w{@var{regs}->}start[@var{i}]} and 2257@code{@w{@var{regs}->}end[@var{i}]} to @math{-1}. 2258 2259For example, when you match the pattern @samp{(a)*b} against 2260the string @samp{b}, you get: 2261 2262@itemize @minus 2263@item 22640 in @code{@w{@var{regs}->}start[0]} and 1 in @code{@w{@var{regs}->}end[0]} 2265 2266@item 2267@math{-1} in @code{@w{@var{regs}->}start[1]} and @math{-1} in @code{@w{@var{regs}->}end[1]} 2268@end itemize 2269 2270@item 2271If the @w{@var{i}-th} group matches a zero-length string, then the 2272function sets @code{@w{@var{regs}->}start[@var{i}]} and 2273@code{@w{@var{regs}->}end[@var{i}]} to the index just beyond that 2274zero-length string. 2275 2276For example, when you match the pattern @samp{(a*)b} against the string 2277@samp{b}, you get: 2278 2279@itemize @minus 2280@item 22810 in @code{@w{@var{regs}->}start[0]} and 1 in @code{@w{@var{regs}->}end[0]} 2282 2283@item 22840 in @code{@w{@var{regs}->}start[1]} and 0 in @code{@w{@var{regs}->}end[1]} 2285@end itemize 2286 2287@ignore 2288The function sets @code{@w{@var{regs}->}start[0]} and 2289@code{@w{@var{regs}->}end[0]} to analogous information about the entire 2290pattern. 2291 2292For example, when you match the pattern @samp{(a*)} against the empty 2293string, you get: 2294 2295@itemize @minus 2296@item 22970 in @code{@w{@var{regs}->}start[0]} and 0 in @code{@w{@var{regs}->}end[0]} 2298 2299@item 23000 in @code{@w{@var{regs}->}start[1]} and 0 in @code{@w{@var{regs}->}end[1]} 2301@end itemize 2302@end ignore 2303 2304@item 2305If an @w{@var{i}-th} group contains a @w{@var{j}-th} group 2306in turn not contained within any other group within group @var{i} and 2307the function reports a match of the @w{@var{i}-th} group, then it 2308records in @code{@w{@var{regs}->}start[@var{j}]} and 2309@code{@w{@var{regs}->}end[@var{j}]} the last match (if it matched) of 2310the @w{@var{j}-th} group. 2311 2312For example, when you match the pattern @samp{((a*)b)*} against the 2313string @samp{abb}, @w{group 2} last matches the empty string, so you 2314get what it previously matched: 2315 2316@itemize @minus 2317@item 23180 in @code{@w{@var{regs}->}start[0]} and 3 in @code{@w{@var{regs}->}end[0]} 2319 2320@item 23212 in @code{@w{@var{regs}->}start[1]} and 3 in @code{@w{@var{regs}->}end[1]} 2322 2323@item 23242 in @code{@w{@var{regs}->}start[2]} and 2 in @code{@w{@var{regs}->}end[2]} 2325@end itemize 2326 2327When you match the pattern @samp{((a)*b)*} against the string 2328@samp{abb}, @w{group 2} doesn't participate in the last match, so you 2329get: 2330 2331@itemize @minus 2332@item 23330 in @code{@w{@var{regs}->}start[0]} and 3 in @code{@w{@var{regs}->}end[0]} 2334 2335@item 23362 in @code{@w{@var{regs}->}start[1]} and 3 in @code{@w{@var{regs}->}end[1]} 2337 2338@item 23390 in @code{@w{@var{regs}->}start[2]} and 1 in @code{@w{@var{regs}->}end[2]} 2340@end itemize 2341 2342@item 2343If an @w{@var{i}-th} group contains a @w{@var{j}-th} group 2344in turn not contained within any other group within group @var{i} 2345and the function sets 2346@code{@w{@var{regs}->}start[@var{i}]} and 2347@code{@w{@var{regs}->}end[@var{i}]} to @math{-1}, then it also sets 2348@code{@w{@var{regs}->}start[@var{j}]} and 2349@code{@w{@var{regs}->}end[@var{j}]} to @math{-1}. 2350 2351For example, when you match the pattern @samp{((a)*b)*c} against the 2352string @samp{c}, you get: 2353 2354@itemize @minus 2355@item 23560 in @code{@w{@var{regs}->}start[0]} and 1 in @code{@w{@var{regs}->}end[0]} 2357 2358@item 2359@math{-1} in @code{@w{@var{regs}->}start[1]} and @math{-1} in @code{@w{@var{regs}->}end[1]} 2360 2361@item 2362@math{-1} in @code{@w{@var{regs}->}start[2]} and @math{-1} in @code{@w{@var{regs}->}end[2]} 2363@end itemize 2364 2365@end itemize 2366 2367@node Freeing GNU Pattern Buffers, , Using Registers, GNU Regex Functions 2368@subsection Freeing GNU Pattern Buffers 2369 2370To free any allocated fields of a pattern buffer, you can use the 2371@sc{posix} function described in @ref{Freeing POSIX Pattern Buffers}, 2372since the type @code{regex_t}---the type for @sc{posix} pattern 2373buffers---is equivalent to the type @code{re_pattern_buffer}. After 2374freeing a pattern buffer, you need to again compile a regular expression 2375in it (@pxref{GNU Regular Expression Compiling}) before passing it to 2376a matching or searching function. 2377 2378 2379@node POSIX Regex Functions, BSD Regex Functions, GNU Regex Functions, Programming with Regex 2380@section POSIX Regex Functions 2381 2382If you're writing code that has to be @sc{posix} compatible, you'll need 2383to use these functions. Their interfaces are as specified by @sc{posix}, 2384draft 1003.2/D11.2. 2385 2386@menu 2387* POSIX Pattern Buffers:: The regex_t type. 2388* POSIX Regular Expression Compiling:: regcomp () 2389* POSIX Matching:: regexec () 2390* Reporting Errors:: regerror () 2391* Using Byte Offsets:: The regmatch_t type. 2392* Freeing POSIX Pattern Buffers:: regfree () 2393@end menu 2394 2395 2396@node POSIX Pattern Buffers, POSIX Regular Expression Compiling, , POSIX Regex Functions 2397@subsection POSIX Pattern Buffers 2398 2399To compile or match a given regular expression the @sc{posix} way, you 2400must supply a pattern buffer exactly the way you do for @sc{gnu} 2401(@pxref{GNU Pattern Buffers}). @sc{posix} pattern buffers have type 2402@code{regex_t}, which is equivalent to the @sc{gnu} pattern buffer 2403type @code{re_pattern_buffer}. 2404 2405 2406@node POSIX Regular Expression Compiling, POSIX Matching, POSIX Pattern Buffers, POSIX Regex Functions 2407@subsection POSIX Regular Expression Compiling 2408 2409With @sc{posix}, you can only search for a given regular expression; you 2410can't match it. To do this, you must first compile it in a 2411pattern buffer, using @code{regcomp}. 2412 2413@ignore 2414Before calling @code{regcomp}, you must initialize this pattern buffer 2415as you do for @sc{gnu} (@pxref{GNU Regular Expression Compiling}). See 2416below, however, for how to choose a syntax with which to compile. 2417@end ignore 2418 2419To compile a pattern buffer, use: 2420 2421@findex regcomp 2422@example 2423int 2424regcomp (regex_t *@var{preg}, const char *@var{regex}, int @var{cflags}) 2425@end example 2426 2427@noindent 2428@var{preg} is the initialized pattern buffer's address, @var{regex} is 2429the regular expression's address, and @var{cflags} is the compilation 2430flags, which Regex considers as a collection of bits. Here are the 2431valid bits, as defined in @file{regex.h}: 2432 2433@table @code 2434 2435@item REG_EXTENDED 2436@vindex REG_EXTENDED 2437says to use @sc{posix} Extended Regular Expression syntax; if this isn't 2438set, then says to use @sc{posix} Basic Regular Expression syntax. 2439@code{regcomp} sets @var{preg}'s @code{syntax} field accordingly. 2440 2441@item REG_ICASE 2442@vindex REG_ICASE 2443@cindex ignoring case 2444says to ignore case; @code{regcomp} sets @var{preg}'s @code{translate} 2445field to a translate table which ignores case, replacing anything you've 2446put there before. 2447 2448@item REG_NOSUB 2449@vindex REG_NOSUB 2450says to set @var{preg}'s @code{no_sub} field; @pxref{POSIX Matching}, 2451for what this means. 2452 2453@item REG_NEWLINE 2454@vindex REG_NEWLINE 2455says that a: 2456 2457@itemize @bullet 2458 2459@item 2460match-any-character operator (@pxref{Match-any-character 2461Operator}) doesn't match a newline. 2462 2463@item 2464nonmatching list not containing a newline (@pxref{List 2465Operators}) matches a newline. 2466 2467@item 2468match-beginning-of-line operator (@pxref{Match-beginning-of-line 2469Operator}) matches the empty string immediately after a newline, 2470regardless of how @code{REG_NOTBOL} is set (@pxref{POSIX Matching}, for 2471an explanation of @code{REG_NOTBOL}). 2472 2473@item 2474match-end-of-line operator (@pxref{Match-beginning-of-line 2475Operator}) matches the empty string immediately before a newline, 2476regardless of how @code{REG_NOTEOL} is set (@pxref{POSIX Matching}, 2477for an explanation of @code{REG_NOTEOL}). 2478 2479@end itemize 2480 2481@end table 2482 2483If @code{regcomp} successfully compiles the regular expression, it 2484returns zero and sets @code{*@var{pattern_buffer}} to the compiled 2485pattern. Except for @code{syntax} (which it sets as explained above), it 2486also sets the same fields the same way as does the @sc{gnu} compiling 2487function (@pxref{GNU Regular Expression Compiling}). 2488 2489If @code{regcomp} can't compile the regular expression, it returns one 2490of the error codes listed here. (Except when noted differently, the 2491syntax of in all examples below is basic regular expression syntax.) 2492 2493@table @code 2494 2495@comment repetitions 2496@item REG_BADRPT 2497For example, the consecutive repetition operators @samp{**} in 2498@samp{a**} are invalid. As another example, if the syntax is extended 2499regular expression syntax, then the repetition operator @samp{*} with 2500nothing on which to operate in @samp{*} is invalid. 2501 2502@item REG_BADBR 2503For example, the @var{count} @samp{-1} in @samp{a\@{-1} is invalid. 2504 2505@item REG_EBRACE 2506For example, @samp{a\@{1} is missing a close-interval operator. 2507 2508@comment lists 2509@item REG_EBRACK 2510For example, @samp{[a} is missing a close-list operator. 2511 2512@item REG_ERANGE 2513For example, the range ending point @samp{z} that collates lower than 2514does its starting point @samp{a} in @samp{[z-a]} is invalid. Also, the 2515range with the character class @samp{[:alpha:]} as its starting point in 2516@samp{[[:alpha:]-|]}. 2517 2518@item REG_ECTYPE 2519For example, the character class name @samp{foo} in @samp{[[:foo:]} is 2520invalid. 2521 2522@comment groups 2523@item REG_EPAREN 2524For example, @samp{a\)} is missing an open-group operator and @samp{\(a} 2525is missing a close-group operator. 2526 2527@item REG_ESUBREG 2528For example, the back reference @samp{\2} that refers to a nonexistent 2529subexpression in @samp{\(a\)\2} is invalid. 2530 2531@comment unfinished business 2532 2533@item REG_EEND 2534Returned when a regular expression causes no other more specific error. 2535 2536@item REG_EESCAPE 2537For example, the trailing backslash @samp{\} in @samp{a\} is invalid, as is the 2538one in @samp{\}. 2539 2540@comment kitchen sink 2541@item REG_BADPAT 2542For example, in the extended regular expression syntax, the empty group 2543@samp{()} in @samp{a()b} is invalid. 2544 2545@comment internal 2546@item REG_ESIZE 2547Returned when a regular expression needs a pattern buffer larger than 254865536 bytes. 2549 2550@item REG_ESPACE 2551Returned when a regular expression makes Regex to run out of memory. 2552 2553@end table 2554 2555 2556@node POSIX Matching, Reporting Errors, POSIX Regular Expression Compiling, POSIX Regex Functions 2557@subsection POSIX Matching 2558 2559Matching the @sc{posix} way means trying to match a null-terminated 2560string starting at its first character. Once you've compiled a pattern 2561into a pattern buffer (@pxref{POSIX Regular Expression Compiling}), you 2562can ask the matcher to match that pattern against a string using: 2563 2564@findex regexec 2565@example 2566int 2567regexec (const regex_t *@var{preg}, const char *@var{string}, 2568 size_t @var{nmatch}, regmatch_t @var{pmatch}[], int @var{eflags}) 2569@end example 2570 2571@noindent 2572@var{preg} is the address of a pattern buffer for a compiled pattern. 2573@var{string} is the string you want to match. 2574 2575@xref{Using Byte Offsets}, for an explanation of @var{pmatch}. If you 2576pass zero for @var{nmatch} or you compiled @var{preg} with the 2577compilation flag @code{REG_NOSUB} set, then @code{regexec} will ignore 2578@var{pmatch}; otherwise, you must allocate it to have at least 2579@var{nmatch} elements. @code{regexec} will record @var{nmatch} byte 2580offsets in @var{pmatch}, and set to @math{-1} any unused elements up to 2581@math{@var{pmatch}@code{[@var{nmatch}]} - 1}. 2582 2583@var{eflags} specifies @dfn{execution flags}---namely, the two bits 2584@code{REG_NOTBOL} and @code{REG_NOTEOL} (defined in @file{regex.h}). If 2585you set @code{REG_NOTBOL}, then the match-beginning-of-line operator 2586(@pxref{Match-beginning-of-line Operator}) always fails to match. 2587This lets you match against pieces of a line, as you would need to if, 2588say, searching for repeated instances of a given pattern in a line; it 2589would work correctly for patterns both with and without 2590match-beginning-of-line operators. @code{REG_NOTEOL} works analogously 2591for the match-end-of-line operator (@pxref{Match-end-of-line 2592Operator}); it exists for symmetry. 2593 2594@code{regexec} tries to find a match for @var{preg} in @var{string} 2595according to the syntax in @var{preg}'s @code{syntax} field. 2596(@xref{POSIX Regular Expression Compiling}, for how to set it.) The 2597function returns zero if the compiled pattern matches @var{string} and 2598@code{REG_NOMATCH} (defined in @file{regex.h}) if it doesn't. 2599 2600@node Reporting Errors, Using Byte Offsets, POSIX Matching, POSIX Regex Functions 2601@subsection Reporting Errors 2602 2603If either @code{regcomp} or @code{regexec} fail, they return a nonzero 2604error code, the possibilities for which are defined in @file{regex.h}. 2605@xref{POSIX Regular Expression Compiling}, and @ref{POSIX Matching}, for 2606what these codes mean. To get an error string corresponding to these 2607codes, you can use: 2608 2609@findex regerror 2610@example 2611size_t 2612regerror (int @var{errcode}, 2613 const regex_t *@var{preg}, 2614 char *@var{errbuf}, 2615 size_t @var{errbuf_size}) 2616@end example 2617 2618@noindent 2619@var{errcode} is an error code, @var{preg} is the address of the pattern 2620buffer which provoked the error, @var{errbuf} is the error buffer, and 2621@var{errbuf_size} is @var{errbuf}'s size. 2622 2623@code{regerror} returns the size in bytes of the error string 2624corresponding to @var{errcode} (including its terminating null). If 2625@var{errbuf} and @var{errbuf_size} are nonzero, it also returns in 2626@var{errbuf} the first @math{@var{errbuf_size} - 1} characters of the 2627error string, followed by a null. 2628@var{errbuf_size} must be a nonnegative number less than or equal to the 2629size in bytes of @var{errbuf}. 2630 2631You can call @code{regerror} with a null @var{errbuf} and a zero 2632@var{errbuf_size} to determine how large @var{errbuf} need be to 2633accommodate @code{regerror}'s error string. 2634 2635@node Using Byte Offsets, Freeing POSIX Pattern Buffers, Reporting Errors, POSIX Regex Functions 2636@subsection Using Byte Offsets 2637 2638In @sc{posix}, variables of type @code{regmatch_t} hold analogous 2639information, but are not identical to, @sc{gnu}'s registers (@pxref{Using 2640Registers}). To get information about registers in @sc{posix}, pass to 2641@code{regexec} a nonzero @var{pmatch} of type @code{regmatch_t}, i.e., 2642the address of a structure of this type, defined in 2643@file{regex.h}: 2644 2645@tindex regmatch_t 2646@example 2647typedef struct 2648@{ 2649 regoff_t rm_so; 2650 regoff_t rm_eo; 2651@} regmatch_t; 2652@end example 2653 2654When reading in @ref{Using Registers}, about how the matching function 2655stores the information into the registers, substitute @var{pmatch} for 2656@var{regs}, @code{@w{@var{pmatch}[@var{i}]->}rm_so} for 2657@code{@w{@var{regs}->}start[@var{i}]} and 2658@code{@w{@var{pmatch}[@var{i}]->}rm_eo} for 2659@code{@w{@var{regs}->}end[@var{i}]}. 2660 2661@node Freeing POSIX Pattern Buffers, , Using Byte Offsets, POSIX Regex Functions 2662@subsection Freeing POSIX Pattern Buffers 2663 2664To free any allocated fields of a pattern buffer, use: 2665 2666@findex regfree 2667@example 2668void 2669regfree (regex_t *@var{preg}) 2670@end example 2671 2672@noindent 2673@var{preg} is the pattern buffer whose allocated fields you want freed. 2674@code{regfree} also sets @var{preg}'s @code{allocated} and @code{used} 2675fields to zero. After freeing a pattern buffer, you need to again 2676compile a regular expression in it (@pxref{POSIX Regular Expression 2677Compiling}) before passing it to the matching function (@pxref{POSIX 2678Matching}). 2679 2680 2681@node BSD Regex Functions, , POSIX Regex Functions, Programming with Regex 2682@section BSD Regex Functions 2683 2684If you're writing code that has to be Berkeley @sc{unix} compatible, 2685you'll need to use these functions whose interfaces are the same as those 2686in Berkeley @sc{unix}. 2687 2688@menu 2689* BSD Regular Expression Compiling:: re_comp () 2690* BSD Searching:: re_exec () 2691@end menu 2692 2693@node BSD Regular Expression Compiling, BSD Searching, , BSD Regex Functions 2694@subsection BSD Regular Expression Compiling 2695 2696With Berkeley @sc{unix}, you can only search for a given regular 2697expression; you can't match one. To search for it, you must first 2698compile it. Before you compile it, you must indicate the regular 2699expression syntax you want it compiled according to by setting the 2700variable @code{re_syntax_options} (declared in @file{regex.h} to some 2701syntax (@pxref{Regular Expression Syntax}). 2702 2703To compile a regular expression use: 2704 2705@findex re_comp 2706@example 2707char * 2708re_comp (char *@var{regex}) 2709@end example 2710 2711@noindent 2712@var{regex} is the address of a null-terminated regular expression. 2713@code{re_comp} uses an internal pattern buffer, so you can use only the 2714most recently compiled pattern buffer. This means that if you want to 2715use a given regular expression that you've already compiled---but it 2716isn't the latest one you've compiled---you'll have to recompile it. If 2717you call @code{re_comp} with the null string (@emph{not} the empty 2718string) as the argument, it doesn't change the contents of the pattern 2719buffer. 2720 2721If @code{re_comp} successfully compiles the regular expression, it 2722returns zero. If it can't compile the regular expression, it returns 2723an error string. @code{re_comp}'s error messages are identical to those 2724of @code{re_compile_pattern} (@pxref{GNU Regular Expression 2725Compiling}). 2726 2727@node BSD Searching, , BSD Regular Expression Compiling, BSD Regex Functions 2728@subsection BSD Searching 2729 2730Searching the Berkeley @sc{unix} way means searching in a string 2731starting at its first character and trying successive positions within 2732it to find a match. Once you've compiled a pattern using @code{re_comp} 2733(@pxref{BSD Regular Expression Compiling}), you can ask Regex 2734to search for that pattern in a string using: 2735 2736@findex re_exec 2737@example 2738int 2739re_exec (char *@var{string}) 2740@end example 2741 2742@noindent 2743@var{string} is the address of the null-terminated string in which you 2744want to search. 2745 2746@code{re_exec} returns either 1 for success or 0 for failure. It 2747automatically uses a @sc{gnu} fastmap (@pxref{Searching with Fastmaps}). 2748 2749 2750@node Copying, Index, Programming with Regex, Top 2751@appendix GNU GENERAL PUBLIC LICENSE 2752@center Version 2, June 1991 2753 2754@display 2755Copyright @copyright{} 1989, 1991 Free Software Foundation, Inc. 2756675 Mass Ave, Cambridge, MA 02139, USA 2757 2758Everyone is permitted to copy and distribute verbatim copies 2759of this license document, but changing it is not allowed. 2760@end display 2761 2762@unnumberedsec Preamble 2763 2764 The licenses for most software are designed to take away your 2765freedom to share and change it. By contrast, the GNU General Public 2766License is intended to guarantee your freedom to share and change free 2767software---to make sure the software is free for all its users. This 2768General Public License applies to most of the Free Software 2769Foundation's software and to any other program whose authors commit to 2770using it. (Some other Free Software Foundation software is covered by 2771the GNU Library General Public License instead.) You can apply it to 2772your programs, too. 2773 2774 When we speak of free software, we are referring to freedom, not 2775price. Our General Public Licenses are designed to make sure that you 2776have the freedom to distribute copies of free software (and charge for 2777this service if you wish), that you receive source code or can get it 2778if you want it, that you can change the software or use pieces of it 2779in new free programs; and that you know you can do these things. 2780 2781 To protect your rights, we need to make restrictions that forbid 2782anyone to deny you these rights or to ask you to surrender the rights. 2783These restrictions translate to certain responsibilities for you if you 2784distribute copies of the software, or if you modify it. 2785 2786 For example, if you distribute copies of such a program, whether 2787gratis or for a fee, you must give the recipients all the rights that 2788you have. You must make sure that they, too, receive or can get the 2789source code. And you must show them these terms so they know their 2790rights. 2791 2792 We protect your rights with two steps: (1) copyright the software, and 2793(2) offer you this license which gives you legal permission to copy, 2794distribute and/or modify the software. 2795 2796 Also, for each author's protection and ours, we want to make certain 2797that everyone understands that there is no warranty for this free 2798software. If the software is modified by someone else and passed on, we 2799want its recipients to know that what they have is not the original, so 2800that any problems introduced by others will not reflect on the original 2801authors' reputations. 2802 2803 Finally, any free program is threatened constantly by software 2804patents. We wish to avoid the danger that redistributors of a free 2805program will individually obtain patent licenses, in effect making the 2806program proprietary. To prevent this, we have made it clear that any 2807patent must be licensed for everyone's free use or not licensed at all. 2808 2809 The precise terms and conditions for copying, distribution and 2810modification follow. 2811 2812@iftex 2813@unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 2814@end iftex 2815@ifinfo 2816@center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 2817@end ifinfo 2818 2819@enumerate 2820@item 2821This License applies to any program or other work which contains 2822a notice placed by the copyright holder saying it may be distributed 2823under the terms of this General Public License. The ``Program'', below, 2824refers to any such program or work, and a ``work based on the Program'' 2825means either the Program or any derivative work under copyright law: 2826that is to say, a work containing the Program or a portion of it, 2827either verbatim or with modifications and/or translated into another 2828language. (Hereinafter, translation is included without limitation in 2829the term ``modification''.) Each licensee is addressed as ``you''. 2830 2831Activities other than copying, distribution and modification are not 2832covered by this License; they are outside its scope. The act of 2833running the Program is not restricted, and the output from the Program 2834is covered only if its contents constitute a work based on the 2835Program (independent of having been made by running the Program). 2836Whether that is true depends on what the Program does. 2837 2838@item 2839You may copy and distribute verbatim copies of the Program's 2840source code as you receive it, in any medium, provided that you 2841conspicuously and appropriately publish on each copy an appropriate 2842copyright notice and disclaimer of warranty; keep intact all the 2843notices that refer to this License and to the absence of any warranty; 2844and give any other recipients of the Program a copy of this License 2845along with the Program. 2846 2847You may charge a fee for the physical act of transferring a copy, and 2848you may at your option offer warranty protection in exchange for a fee. 2849 2850@item 2851You may modify your copy or copies of the Program or any portion 2852of it, thus forming a work based on the Program, and copy and 2853distribute such modifications or work under the terms of Section 1 2854above, provided that you also meet all of these conditions: 2855 2856@enumerate a 2857@item 2858You must cause the modified files to carry prominent notices 2859stating that you changed the files and the date of any change. 2860 2861@item 2862You must cause any work that you distribute or publish, that in 2863whole or in part contains or is derived from the Program or any 2864part thereof, to be licensed as a whole at no charge to all third 2865parties under the terms of this License. 2866 2867@item 2868If the modified program normally reads commands interactively 2869when run, you must cause it, when started running for such 2870interactive use in the most ordinary way, to print or display an 2871announcement including an appropriate copyright notice and a 2872notice that there is no warranty (or else, saying that you provide 2873a warranty) and that users may redistribute the program under 2874these conditions, and telling the user how to view a copy of this 2875License. (Exception: if the Program itself is interactive but 2876does not normally print such an announcement, your work based on 2877the Program is not required to print an announcement.) 2878@end enumerate 2879 2880These requirements apply to the modified work as a whole. If 2881identifiable sections of that work are not derived from the Program, 2882and can be reasonably considered independent and separate works in 2883themselves, then this License, and its terms, do not apply to those 2884sections when you distribute them as separate works. But when you 2885distribute the same sections as part of a whole which is a work based 2886on the Program, the distribution of the whole must be on the terms of 2887this License, whose permissions for other licensees extend to the 2888entire whole, and thus to each and every part regardless of who wrote it. 2889 2890Thus, it is not the intent of this section to claim rights or contest 2891your rights to work written entirely by you; rather, the intent is to 2892exercise the right to control the distribution of derivative or 2893collective works based on the Program. 2894 2895In addition, mere aggregation of another work not based on the Program 2896with the Program (or with a work based on the Program) on a volume of 2897a storage or distribution medium does not bring the other work under 2898the scope of this License. 2899 2900@item 2901You may copy and distribute the Program (or a work based on it, 2902under Section 2) in object code or executable form under the terms of 2903Sections 1 and 2 above provided that you also do one of the following: 2904 2905@enumerate a 2906@item 2907Accompany it with the complete corresponding machine-readable 2908source code, which must be distributed under the terms of Sections 29091 and 2 above on a medium customarily used for software interchange; or, 2910 2911@item 2912Accompany it with a written offer, valid for at least three 2913years, to give any third party, for a charge no more than your 2914cost of physically performing source distribution, a complete 2915machine-readable copy of the corresponding source code, to be 2916distributed under the terms of Sections 1 and 2 above on a medium 2917customarily used for software interchange; or, 2918 2919@item 2920Accompany it with the information you received as to the offer 2921to distribute corresponding source code. (This alternative is 2922allowed only for noncommercial distribution and only if you 2923received the program in object code or executable form with such 2924an offer, in accord with Subsection b above.) 2925@end enumerate 2926 2927The source code for a work means the preferred form of the work for 2928making modifications to it. For an executable work, complete source 2929code means all the source code for all modules it contains, plus any 2930associated interface definition files, plus the scripts used to 2931control compilation and installation of the executable. However, as a 2932special exception, the source code distributed need not include 2933anything that is normally distributed (in either source or binary 2934form) with the major components (compiler, kernel, and so on) of the 2935operating system on which the executable runs, unless that component 2936itself accompanies the executable. 2937 2938If distribution of executable or object code is made by offering 2939access to copy from a designated place, then offering equivalent 2940access to copy the source code from the same place counts as 2941distribution of the source code, even though third parties are not 2942compelled to copy the source along with the object code. 2943 2944@item 2945You may not copy, modify, sublicense, or distribute the Program 2946except as expressly provided under this License. Any attempt 2947otherwise to copy, modify, sublicense or distribute the Program is 2948void, and will automatically terminate your rights under this License. 2949However, parties who have received copies, or rights, from you under 2950this License will not have their licenses terminated so long as such 2951parties remain in full compliance. 2952 2953@item 2954You are not required to accept this License, since you have not 2955signed it. However, nothing else grants you permission to modify or 2956distribute the Program or its derivative works. These actions are 2957prohibited by law if you do not accept this License. Therefore, by 2958modifying or distributing the Program (or any work based on the 2959Program), you indicate your acceptance of this License to do so, and 2960all its terms and conditions for copying, distributing or modifying 2961the Program or works based on it. 2962 2963@item 2964Each time you redistribute the Program (or any work based on the 2965Program), the recipient automatically receives a license from the 2966original licensor to copy, distribute or modify the Program subject to 2967these terms and conditions. You may not impose any further 2968restrictions on the recipients' exercise of the rights granted herein. 2969You are not responsible for enforcing compliance by third parties to 2970this License. 2971 2972@item 2973If, as a consequence of a court judgment or allegation of patent 2974infringement or for any other reason (not limited to patent issues), 2975conditions are imposed on you (whether by court order, agreement or 2976otherwise) that contradict the conditions of this License, they do not 2977excuse you from the conditions of this License. If you cannot 2978distribute so as to satisfy simultaneously your obligations under this 2979License and any other pertinent obligations, then as a consequence you 2980may not distribute the Program at all. For example, if a patent 2981license would not permit royalty-free redistribution of the Program by 2982all those who receive copies directly or indirectly through you, then 2983the only way you could satisfy both it and this License would be to 2984refrain entirely from distribution of the Program. 2985 2986If any portion of this section is held invalid or unenforceable under 2987any particular circumstance, the balance of the section is intended to 2988apply and the section as a whole is intended to apply in other 2989circumstances. 2990 2991It is not the purpose of this section to induce you to infringe any 2992patents or other property right claims or to contest validity of any 2993such claims; this section has the sole purpose of protecting the 2994integrity of the free software distribution system, which is 2995implemented by public license practices. Many people have made 2996generous contributions to the wide range of software distributed 2997through that system in reliance on consistent application of that 2998system; it is up to the author/donor to decide if he or she is willing 2999to distribute software through any other system and a licensee cannot 3000impose that choice. 3001 3002This section is intended to make thoroughly clear what is believed to 3003be a consequence of the rest of this License. 3004 3005@item 3006If the distribution and/or use of the Program is restricted in 3007certain countries either by patents or by copyrighted interfaces, the 3008original copyright holder who places the Program under this License 3009may add an explicit geographical distribution limitation excluding 3010those countries, so that distribution is permitted only in or among 3011countries not thus excluded. In such case, this License incorporates 3012the limitation as if written in the body of this License. 3013 3014@item 3015The Free Software Foundation may publish revised and/or new versions 3016of the General Public License from time to time. Such new versions will 3017be similar in spirit to the present version, but may differ in detail to 3018address new problems or concerns. 3019 3020Each version is given a distinguishing version number. If the Program 3021specifies a version number of this License which applies to it and ``any 3022later version'', you have the option of following the terms and conditions 3023either of that version or of any later version published by the Free 3024Software Foundation. If the Program does not specify a version number of 3025this License, you may choose any version ever published by the Free Software 3026Foundation. 3027 3028@item 3029If you wish to incorporate parts of the Program into other free 3030programs whose distribution conditions are different, write to the author 3031to ask for permission. For software which is copyrighted by the Free 3032Software Foundation, write to the Free Software Foundation; we sometimes 3033make exceptions for this. Our decision will be guided by the two goals 3034of preserving the free status of all derivatives of our free software and 3035of promoting the sharing and reuse of software generally. 3036 3037@iftex 3038@heading NO WARRANTY 3039@end iftex 3040@ifinfo 3041@center NO WARRANTY 3042@end ifinfo 3043 3044@item 3045BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 3046FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 3047OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 3048PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 3049OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 3050MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 3051TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 3052PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 3053REPAIR OR CORRECTION. 3054 3055@item 3056IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 3057WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 3058REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 3059INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 3060OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 3061TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 3062YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 3063PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 3064POSSIBILITY OF SUCH DAMAGES. 3065@end enumerate 3066 3067@iftex 3068@heading END OF TERMS AND CONDITIONS 3069@end iftex 3070@ifinfo 3071@center END OF TERMS AND CONDITIONS 3072@end ifinfo 3073 3074@page 3075@unnumberedsec Appendix: How to Apply These Terms to Your New Programs 3076 3077 If you develop a new program, and you want it to be of the greatest 3078possible use to the public, the best way to achieve this is to make it 3079free software which everyone can redistribute and change under these terms. 3080 3081 To do so, attach the following notices to the program. It is safest 3082to attach them to the start of each source file to most effectively 3083convey the exclusion of warranty; and each file should have at least 3084the ``copyright'' line and a pointer to where the full notice is found. 3085 3086@smallexample 3087@var{one line to give the program's name and a brief idea of what it does.} 3088Copyright (C) 19@var{yy} @var{name of author} 3089 3090This program is free software; you can redistribute it and/or modify 3091it under the terms of the GNU General Public License as published by 3092the Free Software Foundation; either version 2 of the License, or 3093(at your option) any later version. 3094 3095This program is distributed in the hope that it will be useful, 3096but WITHOUT ANY WARRANTY; without even the implied warranty of 3097MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3098GNU General Public License for more details. 3099 3100You should have received a copy of the GNU General Public License 3101along with this program; if not, write to the Free Software 3102Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 3103@end smallexample 3104 3105Also add information on how to contact you by electronic and paper mail. 3106 3107If the program is interactive, make it output a short notice like this 3108when it starts in an interactive mode: 3109 3110@smallexample 3111Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author} 3112Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 3113This is free software, and you are welcome to redistribute it 3114under certain conditions; type `show c' for details. 3115@end smallexample 3116 3117The hypothetical commands @samp{show w} and @samp{show c} should show 3118the appropriate parts of the General Public License. Of course, the 3119commands you use may be called something other than @samp{show w} and 3120@samp{show c}; they could even be mouse-clicks or menu items---whatever 3121suits your program. 3122 3123You should also get your employer (if you work as a programmer) or your 3124school, if any, to sign a ``copyright disclaimer'' for the program, if 3125necessary. Here is a sample; alter the names: 3126 3127@example 3128Yoyodyne, Inc., hereby disclaims all copyright interest in the program 3129`Gnomovision' (which makes passes at compilers) written by James Hacker. 3130 3131@var{signature of Ty Coon}, 1 April 1989 3132Ty Coon, President of Vice 3133@end example 3134 3135This General Public License does not permit incorporating your program into 3136proprietary programs. If your program is a subroutine library, you may 3137consider it more useful to permit linking proprietary applications with the 3138library. If this is what you want to do, use the GNU Library General 3139Public License instead of this License. 3140 3141 3142@node Index, , Copying, Top 3143@unnumbered Index 3144 3145@printindex cp 3146 3147@contents 3148 3149@bye 3150