~statik/ubuntu/maverick/erlang/erlang-merge-testing

« back to all changes in this revision

Viewing changes to lib/common_test/priv/rx-1.5/rgx-perf/longdotstr/data.c

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-05-01 10:14:38 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090501101438-6qlr6rsdxgyzrg2z
Tags: 1:13.b-dfsg-2
* Cleaned up patches: removed unneeded patch which helped to support
  different SCTP library versions, made sure that changes for m68k
  architecture applied only when building on this architecture.
* Removed duplicated information from binary packages descriptions.
* Don't require libsctp-dev build-dependency on solaris-i386 architecture
  which allows to build Erlang on Nexenta (thanks to Tim Spriggs for
  the suggestion).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
char data[] =
2
 
"\\input texinfo   @c -*-texinfo-*-"
3
 
"@c %**start of header"
4
 
"@setfilename rx.info"
5
 
"@settitle Rx"
6
 
"@setchapternewpage odd"
7
 
"@c %**end of header"
8
 
""
9
 
"@c  This title page illustrates only one of the"
10
 
"@c  two methods of forming a title page."
11
 
""
12
 
"@titlepage"
13
 
"@title Rx"
14
 
"@author Tom Lord"
15
 
""
16
 
"@center except the chapter \"Posix Entry Points\" "
17
 
"@center from @emph{The GNU C Library} reference manual"
18
 
"@center by Sandra Loosemore"
19
 
"@center with"
20
 
"@center Richard M. Stallman, Roland McGrath, and Andrew Oram"
21
 
""
22
 
"@c  The following two commands"
23
 
"@c  start the copyright page."
24
 
"@page"
25
 
"@vskip 0pt plus 1filll"
26
 
"Copyright @copyright{} 1995 Cygnus Support"
27
 
""
28
 
"except the chapter \"Posix Entry Points\" which is:"
29
 
""
30
 
"Copyright @copyright{} 1995 Free Software Foundation, Inc."
31
 
""
32
 
"Permission is granted to make and distribute verbatim copies of"
33
 
"this manual provided the copyright notice and this permission notice"
34
 
"are preserved on all copies."
35
 
""
36
 
"Permission is granted to copy and distribute modified versions of this"
37
 
"manual under the conditions for verbatim copying, provided that the entire"
38
 
"resulting derived work is distributed under the terms of a permission"
39
 
"notice identical to this one."
40
 
""
41
 
"Permission is granted to copy and distribute translations of this manual"
42
 
"into another language, under the above conditions for modified versions,"
43
 
"except that this permission notice may be stated in a translation approved"
44
 
"by the author."
45
 
""
46
 
"@end titlepage"
47
 
""
48
 
"@node Top, Copying, (dir), (dir)"
49
 
"@top Rx"
50
 
""
51
 
"This document describes Rx."
52
 
""
53
 
"@menu"
54
 
"* Copying::                            Sharing is good."
55
 
"* Overview::                           Fnord"
56
 
"* Posix Basic Regular Expressions::    A popular regexp syntax."
57
 
"* Posix Entry Points::                 The POSIX way to regexp."
58
 
"* Beyond POSIX::                       Hints about cool features."
59
 
"* Rx Theory::                          Hints about how it works."
60
 
"@end menu"
61
 
""
62
 
"@node Copying, Overview, Top, Top"
63
 
"@chapter Copying"
64
 
""
65
 
"@center Copyright (C) 1996"
66
 
"@center Tom Lord"
67
 
"@center Berkeley, CA USA"
68
 
""
69
 
"@center except portions of \"POSIX Regex Functions\" which are"
70
 
"@center Copyright (C) 1995"
71
 
"@center Free Software Foundation, Inc."
72
 
""
73
 
"@noindent"
74
 
"Permission to use, copy, modify, distribute, and sell this software and"
75
 
"its documentation for any purpose is hereby granted without fee,"
76
 
"provided that the above copyright notice appear in all copies and that"
77
 
"both that copyright notice and this permission notice appear in"
78
 
"supporting documentation."
79
 
""
80
 
"@center NO WARRANTY"
81
 
""
82
 
"@noindent"
83
 
"BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR"
84
 
"THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN"
85
 
"OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES"
86
 
"PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER"
87
 
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED"
88
 
"WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE"
89
 
"ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH"
90
 
"YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL"
91
 
"NECESSARY SERVICING, REPAIR OR CORRECTION."
92
 
""
93
 
"@noindent"
94
 
"IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING"
95
 
"WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR"
96
 
"REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR"
97
 
"DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL"
98
 
"DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM"
99
 
"(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED"
100
 
"INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF"
101
 
"THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR"
102
 
"OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES."
103
 
""
104
 
"@node  Overview, Posix Basic Regular Expressions, Copying, Top"
105
 
"@chapter Overview"
106
 
""
107
 
"Nothing to say here, yet."
108
 
""
109
 
"@node  Posix Basic Regular Expressions, Posix Entry Points, Overview, Top"
110
 
"@chapter Posix Basic Regular Expressions"
111
 
""
112
 
"The Posix Basic Regular Expression language is a notation for describing"
113
 
"textual patterns.  Regexps are typically used by comparing them to a"
114
 
"string to see if that string matches the pattern, or by searching within"
115
 
"a string for a substring that matches."
116
 
""
117
 
"This chapter introduces the Posix regexp notation.  This is not a formal"
118
 
"or precise definition of Posix regexps -- it is an intuitive and"
119
 
"hopefully expository description of them."
120
 
""
121
 
"@menu"
122
 
"* An Introduction to Regexps::  "
123
 
"* Literal Regexps::             "
124
 
"* Character Sets::              "
125
 
"* Subexpressions::              "
126
 
"* Repeated Subexpressions::     "
127
 
"* Optional Subexpressions::     "
128
 
"* Counted Subexpressions::      "
129
 
"* Alternative Subexpressions::  "
130
 
"* Backreferences::              "
131
 
"* A Summary of Regexp Syntax::  "
132
 
"* Ambiguous Patterns::          "
133
 
"@end menu"
134
 
""
135
 
""
136
 
"@node  An Introduction to Regexps, Literal Regexps, Posix Basic Regular Expressions, Posix Basic Regular Expressions"
137
 
"@section An Introduction to Regexps"
138
 
""
139
 
"In the simplest cases, a regexp is just a literal string that must"
140
 
"match exactly.   For example, the pattern:"
141
 
""
142
 
"@example"
143
 
"regexp"
144
 
"@end example"
145
 
""
146
 
"matches the string \"regexp\" and no others."
147
 
""
148
 
"Some characters have a special meaning when they occur in a regexp."
149
 
"They aren't matched literally as in the previous example, but instead"
150
 
"denote a more general pattern.   For example, the character @code{*}"
151
 
"is used to indicate that the preceeding element of a regexp may be"
152
 
"repeated 0, 1, or more times.  In the pattern:"
153
 
""
154
 
"@example"
155
 
"smooo*th"
156
 
"@end example"
157
 
""
158
 
"the @code{*} indicates that the preceeding @code{o} can be repeated 0 or"
159
 
"more times.   So the pattern matches:"
160
 
""
161
 
"@example"
162
 
"smooth"
163
 
"smoooth"
164
 
"smooooth"
165
 
"smoooooth"
166
 
"..."
167
 
"@end example"
168
 
""
169
 
"Suppose you want to write a pattern that literally matches a special"
170
 
"character like @code{*} -- in other words, you don't want to @code{*} to"
171
 
"indicate a permissible repetition, but to match @code{*} literally.  This"
172
 
"is accomplished by quoting the special character with a backslash.  "
173
 
"The pattern:"
174
 
""
175
 
"@example"
176
 
"smoo\\*th"
177
 
"@end example"
178
 
""
179
 
"matches the string:"
180
 
""
181
 
"@example"
182
 
"smoo*th"
183
 
"@end example"
184
 
""
185
 
""
186
 
"and no other strings."
187
 
""
188
 
"In five cases, the pattern is reversed -- a backslash makes the"
189
 
"character special instead of making a special character normal.  The"
190
 
"characters @code{+}, @code{?}, @code{|}, @code{(}, and @code{)} are"
191
 
"normal but the sequences @code{\\+}, @code{\\?}, @code{\\|}, @code{\\(}, and"
192
 
"@code{\\)} are special (their meaning is described later)."
193
 
""
194
 
"The remaining sections of this chapter introduce and explain the various"
195
 
"special characters that can occur in regexps."
196
 
""
197
 
"@node  Literal Regexps, Character Sets, An Introduction to Regexps, Posix Basic Regular Expressions"
198
 
"@section Literal Regexps"
199
 
""
200
 
"A literal regexp is a string which contains no special characters."
201
 
"A literal regexp matches an identical string, but no other characters."
202
 
"For example:"
203
 
""
204
 
"@example"
205
 
"literally"
206
 
"@end example"
207
 
""
208
 
"matches"
209
 
""
210
 
"@example"
211
 
"literally"
212
 
"@end example"
213
 
""
214
 
"and nothing else."
215
 
""
216
 
"Generally, whitespace characters, numbers, and letters are not special."
217
 
"Some punctuation characters are special and some are not (the syntax"
218
 
"summary at the end of this chapter makes a convenient reference for"
219
 
"which characters are special and which aren't)."
220
 
""
221
 
""
222
 
"@node  Character Sets, Subexpressions, Literal Regexps, Posix Basic Regular Expressions"
223
 
"@section Character Sets"
224
 
""
225
 
"This section introduces the special characters @code{.} and @code{[}."
226
 
""
227
 
"@code{.} matches any character except the NULL character.  For example:"
228
 
""
229
 
"@example"
230
 
"p.ck"
231
 
"@end example"
232
 
""
233
 
"matches"
234
 
""
235
 
"@example"
236
 
"pick"
237
 
"pack"
238
 
"puck"
239
 
"pbck"
240
 
"pcck"
241
 
"p.ck"
242
 
""
243
 
"..."
244
 
"@end example"
245
 
""
246
 
""
247
 
"@code{[} begins a @dfn{character set}.  A character set is similar to"
248
 
"@code{.} in that it matches not a single, literal character, but any"
249
 
"of a set of characters.   @code{[} is different from @code{.} in that"
250
 
"with @code{[}, you define the set of characters explicitly."
251
 
""
252
 
"There are three basic forms a character set can take."
253
 
""
254
 
"In the first form, the character set is spelled out:"
255
 
""
256
 
"@example"
257
 
"[<cset-spec>]  -- every character in <cset-spec> is in the set."
258
 
"@end example"
259
 
""
260
 
"In the second form, the character set indicated is the negation of"
261
 
"a character set is explicitly spelled out:"
262
 
""
263
 
"@example"
264
 
"[^<cset-spec>] -- every character *not* in <cset-spec> is in the set."
265
 
"@end example"
266
 
""
267
 
"A @code{<cset-spec>} is more or less an explicit enumeration of a set"
268
 
"of characters.  It can be written as a string of individual characters:"
269
 
""
270
 
"@example"
271
 
"[aeiou]"
272
 
"@end example"
273
 
""
274
 
"or as a range of characters:"
275
 
""
276
 
"@example"
277
 
"[0-9]"
278
 
"@end example"
279
 
""
280
 
"These two forms can be mixed:"
281
 
""
282
 
"@example"
283
 
"[A-za-z0-9_$]"
284
 
"@end example"
285
 
""
286
 
"Note that special regexp characters (such as @code{*}) are @emph{not}"
287
 
"special within a character set.  @code{-}, as illustrated above,"
288
 
"@emph{is} special, except, as illustrated below, when it is the first"
289
 
"character mentioned."
290
 
""
291
 
"This is a four-character set:"
292
 
""
293
 
"@example"
294
 
"[-+*/]"
295
 
"@end example"
296
 
""
297
 
"The third form of a character set makes use of a pre-defined \"character"
298
 
"class\":"
299
 
""
300
 
"@example"
301
 
"[[:class-name:]] -- every character described by class-name is in the set."
302
 
"@end example"
303
 
""
304
 
"The supported character classes are:"
305
 
""
306
 
"@example"
307
 
"alnum  - the set of alpha-numeric characters"
308
 
"alpha  - the set of alphabetic characters"
309
 
"blank  - tab and space"
310
 
"cntrl  - the control characters"
311
 
"digit  - decimal digits"
312
 
"graph  - all printable characters except space"
313
 
"lower  - lower case letters"
314
 
"print  - the \"printable\" characters"
315
 
"punct  - punctuation"
316
 
"space  - whitespace characters"
317
 
"upper  - upper case letters"
318
 
"xdigit - hexidecimal digits"
319
 
"@end example"
320
 
""
321
 
"Finally, character class sets can also be inverted:"
322
 
""
323
 
"@example"
324
 
"[^[:space:]] - all non-whitespace characters"
325
 
"@end example"
326
 
""
327
 
"Character sets can be used in a regular expression anywhere a literal"
328
 
"character can."
329
 
""
330
 
"@node  Subexpressions, Repeated Subexpressions, Character Sets, Posix Basic Regular Expressions"
331
 
"@section Subexpressions"
332
 
""
333
 
"A subexpression is a regular expression enclosed in @code{\\(} and"
334
 
"@code{\\)}.  A subexpression can be used anywhere a single character or"
335
 
"character set can be used."
336
 
""
337
 
"Subexpressions are useful for grouping regexp constructs.  For example,"
338
 
"the repeat operator, @code{*}, usually applies to just the preceeding"
339
 
"character.   Recall that:"
340
 
""
341
 
"@example"
342
 
"smooo*th"
343
 
"@end example"
344
 
""
345
 
"matches"
346
 
""
347
 
"@example"
348
 
"smooth"
349
 
"smoooth"
350
 
"..."
351
 
"@end example"
352
 
""
353
 
""
354
 
"Using a subexpression, we can apply @code{*} to a longer string:"
355
 
""
356
 
"@example"
357
 
"banan\\(an\\)*a"
358
 
"@end example"
359
 
""
360
 
"matches"
361
 
""
362
 
"@example"
363
 
"banana"
364
 
"bananana"
365
 
"banananana"
366
 
"..."
367
 
"@end example"
368
 
""
369
 
"Subexpressions also have a special meaning with regard to backreferences"
370
 
"and substitutions (see @xref{Backreferences})."
371
 
""
372
 
""
373
 
"@node  Repeated Subexpressions, Optional Subexpressions, Subexpressions, Posix Basic Regular Expressions"
374
 
"@section Repeated Subexpressions"
375
 
""
376
 
""
377
 
"@code{*} is the repeat operator.  It applies to the preceeding"
378
 
"character, character set, subexpression or backreference.  It indicates"
379
 
"that the preceeding element can be matched 0 or more times:"
380
 
""
381
 
"@example"
382
 
"bana\\(na\\)*"
383
 
"@end example"
384
 
""
385
 
"matches"
386
 
""
387
 
"@example"
388
 
"bana"
389
 
"banana"
390
 
"bananana"
391
 
"banananana"
392
 
"..."
393
 
"@end example"
394
 
""
395
 
""
396
 
""
397
 
"@code{\\+} is similar to @code{*} except that @code{\\+} requires the"
398
 
"preceeding element to be matched at least once.  So while:"
399
 
""
400
 
"@example"
401
 
"bana\\(na\\)*"
402
 
"@end example"
403
 
""
404
 
"matches"
405
 
""
406
 
"@example"
407
 
"bana"
408
 
"@end example"
409
 
""
410
 
"@example"
411
 
"bana(na\\)\\+"
412
 
"@end example"
413
 
""
414
 
"does not.   Both match "
415
 
""
416
 
"@example"
417
 
"banana"
418
 
"bananana"
419
 
"banananana"
420
 
"..."
421
 
"@end example"
422
 
""
423
 
"Thus, @code{bana\\(na\\)+} is short-hand for @code{banana\\(na\\)*}."
424
 
""
425
 
""
426
 
"@node  Optional Subexpressions, Counted Subexpressions, Repeated Subexpressions, Posix Basic Regular Expressions"
427
 
"@section Optional Subexpressions"
428
 
""
429
 
"@code{\\?} indicates that the preceeding character, character set, or"
430
 
"subexpression is optional.  It is permitted to match, or to be skipped:"
431
 
""
432
 
"@example"
433
 
"CSNY\\?"
434
 
"@end example"
435
 
""
436
 
"matches both"
437
 
""
438
 
"@example"
439
 
"CSN"
440
 
"@end example"
441
 
""
442
 
"and "
443
 
""
444
 
"@example"
445
 
"CSNY"
446
 
"@end example"
447
 
""
448
 
""
449
 
"@node  Counted Subexpressions, Alternative Subexpressions, Optional Subexpressions, Posix Basic Regular Expressions"
450
 
"@section Counted Subexpressions"
451
 
""
452
 
"An interval expression, @code{@{m,n@}} where @code{m} and @code{n} are"
453
 
"non-negative integers with @code{n >= m}, applies to the preceeding"
454
 
"character, character set, subexpression or backreference.  It indicates"
455
 
"that the preceeding element must match at least @code{m} times and may"
456
 
"match as many as @code{n} times."
457
 
""
458
 
"For example:"
459
 
""
460
 
"@example"
461
 
"c\\([ad]\\)@{1,4@}"
462
 
"@end example"
463
 
""
464
 
"matches"
465
 
""
466
 
"@example"
467
 
"car"
468
 
"cdr"
469
 
"caar"
470
 
"cdar"
471
 
"..."
472
 
"caaar"
473
 
"cdaar"
474
 
"..."
475
 
"cadddr"
476
 
"cddddr"
477
 
"@end example"
478
 
""
479
 
""
480
 
""
481
 
"@node  Alternative Subexpressions, Backreferences, Counted Subexpressions, Posix Basic Regular Expressions"
482
 
"@section Alternative Subexpressions"
483
 
""
484
 
"An alternative is written:"
485
 
""
486
 
"@example"
487
 
"regexp-1\\|regexp-2\\|regexp-3\\|..."
488
 
"@end example"
489
 
""
490
 
"It matches anything matched by some @code{regexp-n}.  For example:"
491
 
""
492
 
"@example"
493
 
"Crosby, Stills, \\(and Nash\\|Nash, and Young\\)"
494
 
"@end example"
495
 
""
496
 
"matches"
497
 
""
498
 
"@example"
499
 
"Crosby, Stills, and Nash"
500
 
"@end example"
501
 
""
502
 
"and"
503
 
""
504
 
"@example"
505
 
"Crosby, Stills, Nash, and Young"
506
 
"@end example"
507
 
""
508
 
""
509
 
"@node  Backreferences, A Summary of Regexp Syntax, Alternative Subexpressions, Posix Basic Regular Expressions"
510
 
"@section Backreferences, Extractions and Substitutions"
511
 
""
512
 
"A backreference is written @code{\\n} where @code{n} is some single digit"
513
 
"other than 0.  To be a valid backreference, there must be at least"
514
 
"@code{n} parenthesized subexpressions in the pattern prior to the"
515
 
"backreference."
516
 
""
517
 
"A backreference matches a literal copy of whatever was matched by the"
518
 
"corresponding subexpression.  For example,"
519
 
""
520
 
"@example"
521
 
"\\(.*\\)-\\1"
522
 
"@end example"
523
 
""
524
 
"matches:"
525
 
""
526
 
"@example"
527
 
"go-go"
528
 
"ha-ha"
529
 
"wakka-wakka"
530
 
"..."
531
 
"@end example"
532
 
""
533
 
"In some applications, subexpressions are used to extract substrings."
534
 
"For example, Emacs has the functions @code{match-beginnning} and"
535
 
"@code{match-end} which report the positions of strings matched by"
536
 
"subexpressions.  These functions use the same numbering scheme for"
537
 
"subexpressions as backreferences, with the additional rule that"
538
 
"subexpression 0 is defined to be the whole regexp."
539
 
""
540
 
"In some applications, subexpressions are used in string substitution."
541
 
"This again uses the backreference numbering scheme. For example, this sed"
542
 
"command:"
543
 
""
544
 
"@example"
545
 
"s/From:.*<\\(.*\\)>/To: \\1/"
546
 
"@end example"
547
 
""
548
 
"first matches the line:"
549
 
""
550
 
"@example"
551
 
"From: Joe Schmoe <schmoe@@uspringfield.edu>"
552
 
"@end example"
553
 
""
554
 
"when it does, subexpression 1 matches \"schmoe@@uspringfield.edu\"."
555
 
"The command replaces the matched line with \"To: \\1\" after doing"
556
 
"subexpression substitution on it to get:"
557
 
""
558
 
"@example"
559
 
"To: schmoe@@uspringfield.edu"
560
 
"@end example"
561
 
""
562
 
""
563
 
"@node  A Summary of Regexp Syntax, Ambiguous Patterns, Backreferences, Posix Basic Regular Expressions"
564
 
"@section A Summary of Regexp Syntax"
565
 
""
566
 
"In summary, regexps can be:"
567
 
""
568
 
"@code{abcd} -- matching a string literally"
569
 
""
570
 
"@code{.} -- matching everything except NULL"
571
 
""
572
 
"@code{[a-z_?]}, @code{^[a-z_?]}, @code{[[:alpha:]]} and"
573
 
"@code{[^[:alpha:]]} -- matching character sets"
574
 
""
575
 
"@code{\\(subexp\\)} -- grouping an expression into a subexpression."
576
 
""
577
 
"@code{\\n} -- match a copy of whatever was matched by the nth subexpression."
578
 
""
579
 
"The following special characters and sequences can be applied to a"
580
 
"character, character set, subexpression, or backreference:"
581
 
""
582
 
"@code{*} -- repeat the preceeding element 0 or more times."
583
 
""
584
 
"@code{\\+} -- repeat the preceeding element 1 or more times."
585
 
""
586
 
"@code{\\?} -- match the preceeding element 0 or 1 time."
587
 
""
588
 
"@code{@{m,n@}} -- match the preceeding element at least @code{m}, and as"
589
 
"many as @code{n} times."
590
 
""
591
 
"@code{regexp-1\\|regexp-2\\|..} -- match any regexp-n."
592
 
""
593
 
"A special character, like @code{.} or @code{*} can be made into a literal"
594
 
"character by prefixing it with @code{\\}."
595
 
""
596
 
"A special sequence, like @code{\\+} or @code{\\?} can be made into a"
597
 
"literal character by dropping the @code{\\}."
598
 
""
599
 
""
600
 
"@node  Ambiguous Patterns,  , A Summary of Regexp Syntax, Posix Basic Regular Expressions"
601
 
"@section Ambiguous Patterns"
602
 
""
603
 
"Sometimes a regular expression appears to be ambiguous.  For example, suppose"
604
 
"we compare the pattern:"
605
 
""
606
 
"@example"
607
 
"begin\\|beginning"
608
 
"@end example"
609
 
""
610
 
"to the string"
611
 
""
612
 
"@example"
613
 
"beginning"
614
 
"@end example"
615
 
""
616
 
"either just the first 5 characters will match, or the whole string will match."
617
 
""
618
 
"In every case like this, the longer match is preferred.  The whole"
619
 
"string will match."
620
 
""
621
 
"Sometimes there is ambiguity not about how many characters to match, but"
622
 
"where the subexpressions occur within the match.  This can effect"
623
 
"extraction functions like Emacs' @code{match-beginning} or rewrite"
624
 
"functions like sed's @code{s} command.  For example, consider matching"
625
 
"the pattern:"
626
 
""
627
 
"@example"
628
 
"b\\(\\[^q]*\\)\\(ing\\)?"
629
 
"@end example"
630
 
""
631
 
"against the string"
632
 
""
633
 
"@example"
634
 
"beginning"
635
 
"@end example"
636
 
""
637
 
"One possibility is that the first subexpression matches \"eginning\" and"
638
 
"the second is skipped.  Another possibility is that the first"
639
 
"subexpression matches \"eginn\" and the second matches \"ing\"."
640
 
""
641
 
"The rule is that consistant with matching as many characters as "
642
 
"possible, the length of lower numbered subexpressions is maximized"
643
 
"in preference to maximizing the length of later subexpressions."
644
 
""
645
 
"In the case of the above example, the two possible matches are equal in"
646
 
"overall length.  Therefore, it comes down to maximizing the"
647
 
"lower-numbered subexpression, \\1.  The correct answer is that \\1 matches"
648
 
"\"eginning\" and \\2 is skipped."
649
 
""
650
 
"@node Posix Entry Points, Beyond POSIX, Posix Basic Regular Expressions, Top"
651
 
"@chapter Posix Entry Points"
652
 
""
653
 
"This section is excerpted from @emph{The GNU C Library} reference manual"
654
 
"by Sandra Loosemore with Richard M. Stallman, Roland McGrath, and Andrew"
655
 
"Oram."
656
 
""
657
 
"The GNU C library supports the standard POSIX.2 interface.  Programs"
658
 
"using this interface should include the header file @file{rxposix.h}."
659
 
""
660
 
"@menu"
661
 
"* POSIX Regexp Compilation::    Using @code{regcomp} to prepare to match."
662
 
"* Flags for POSIX Regexps::     Syntax variations for @code{regcomp}."
663
 
"* Matching POSIX Regexps::      Using @code{regexec} to match the compiled"
664
 
"                                  pattern that you get from @code{regcomp}."
665
 
"* Regexp Subexpressions::       Finding which parts of the string were matched."
666
 
"* Subexpression Complications::  Find points of which parts were matched."
667
 
"* Regexp Cleanup::              Freeing storage; reporting errors."
668
 
"@end menu"
669
 
""
670
 
"@node POSIX Regexp Compilation, Flags for POSIX Regexps, Posix Entry Points, Posix Entry Points"
671
 
"@section POSIX Regular Expression Compilation"
672
 
""
673
 
"Before you can actually match a regular expression, you must"
674
 
"@dfn{compile} it.  This is not true compilation---it produces a special"
675
 
"data structure, not machine instructions.  But it is like ordinary"
676
 
"compilation in that its purpose is to enable you to ``execute'' the"
677
 
"pattern fast.  (@xref{Matching POSIX Regexps}, for how to use the"
678
 
"compiled regular expression for matching.)"
679
 
""
680
 
"There is a special data type for compiled regular expressions:"
681
 
""
682
 
"@comment regex.h"
683
 
"@comment POSIX.2"
684
 
"@deftp {Data Type} regex_t"
685
 
"This type of object holds a compiled regular expression."
686
 
"It is actually a structure.  It has just one field that your programs"
687
 
"should look at:"
688
 
""
689
 
"@table @code"
690
 
"@item re_nsub"
691
 
"This field holds the number of parenthetical subexpressions in the"
692
 
"regular expression that was compiled."
693
 
"@end table"
694
 
""
695
 
"There are several other fields, but we don't describe them here, because"
696
 
"only the functions in the library should use them."
697
 
"@end deftp"
698
 
""
699
 
"After you create a @code{regex_t} object, you can compile a regular"
700
 
"expression into it by calling @code{regcomp}."
701
 
""
702
 
"@comment regex.h"
703
 
"@comment POSIX.2"
704
 
"@deftypefun int regcomp (regex_t *@var{compiled}, const char *@var{pattern}, int @var{cflags})"
705
 
"@deftypefunx int regncomp (regex_t *@var{compiled}, int len, const char *@var{pattern}, int @var{cflags})"
706
 
"The function @code{regcomp} ``compiles'' a regular expression into a"
707
 
"data structure that you can use with @code{regexec} to match against a"
708
 
"string.  The compiled regular expression format is designed for"
709
 
"efficient matching.  @code{regcomp} stores it into @code{*@var{compiled}}."
710
 
""
711
 
"The parameter @var{pattern} points to the regular expression to be"
712
 
"compiled.  When using @code{regcomp}, @var{pattern} must be"
713
 
"0-terminated.  When using @code{regncomp}, @var{pattern} must be"
714
 
"@var{len} characters long."
715
 
""
716
 
"@code{regncomp} is not a standard function;  strictly POSIX programs"
717
 
"should avoid using it."
718
 
""
719
 
"It's up to you to allocate an object of type @code{regex_t} and pass its"
720
 
"address to @code{regcomp}."
721
 
""
722
 
"The argument @var{cflags} lets you specify various options that control"
723
 
"the syntax and semantics of regular expressions.  @xref{Flags for POSIX"
724
 
"Regexps}."
725
 
""
726
 
"If you use the flag @code{REG_NOSUB}, then @code{regcomp} omits from"
727
 
"the compiled regular expression the information necessary to record"
728
 
"how subexpressions actually match.  In this case, you might as well"
729
 
"pass @code{0} for the @var{matchptr} and @var{nmatch} arguments when"
730
 
"you call @code{regexec}."
731
 
""
732
 
"If you don't use @code{REG_NOSUB}, then the compiled regular expression"
733
 
"does have the capacity to record how subexpressions match.  Also,"
734
 
"@code{regcomp} tells you how many subexpressions @var{pattern} has, by"
735
 
"storing the number in @code{@var{compiled}->re_nsub}.  You can use that"
736
 
"value to decide how long an array to allocate to hold information about"
737
 
"subexpression matches."
738
 
""
739
 
"@code{regcomp} returns @code{0} if it succeeds in compiling the regular"
740
 
"expression; otherwise, it returns a nonzero error code (see the table"
741
 
"below).  You can use @code{regerror} to produce an error message string"
742
 
"describing the reason for a nonzero value; see @ref{Regexp Cleanup}."
743
 
""
744
 
"@end deftypefun"
745
 
""
746
 
"Here are the possible nonzero values that @code{regcomp} can return:"
747
 
""
748
 
"@table @code"
749
 
"@comment regex.h"
750
 
"@comment POSIX.2"
751
 
"@item REG_BADBR"
752
 
"There was an invalid @samp{\\@{@dots{}\\@}} construct in the regular"
753
 
"expression.  A valid @samp{\\@{@dots{}\\@}} construct must contain either"
754
 
"a single number, or two numbers in increasing order separated by a"
755
 
"comma."
756
 
""
757
 
"@comment regex.h"
758
 
"@comment POSIX.2"
759
 
"@item REG_BADPAT"
760
 
"There was a syntax error in the regular expression."
761
 
""
762
 
"@comment regex.h"
763
 
"@comment POSIX.2"
764
 
"@item REG_BADRPT"
765
 
"A repetition operator such as @samp{?} or @samp{*} appeared in a bad"
766
 
"position (with no preceding subexpression to act on)."
767
 
""
768
 
"@comment regex.h"
769
 
"@comment POSIX.2"
770
 
"@item REG_ECOLLATE"
771
 
"The regular expression referred to an invalid collating element (one not"
772
 
"defined in the current locale for string collation).  "
773
 
""
774
 
"@comment regex.h"
775
 
"@comment POSIX.2"
776
 
"@item REG_ECTYPE"
777
 
"The regular expression referred to an invalid character class name."
778
 
""
779
 
"@comment regex.h"
780
 
"@comment POSIX.2"
781
 
"@item REG_EESCAPE"
782
 
"The regular expression ended with @samp{\\}."
783
 
""
784
 
"@comment regex.h"
785
 
"@comment POSIX.2"
786
 
"@item REG_ESUBREG"
787
 
"There was an invalid number in the @samp{\\@var{digit}} construct."
788
 
""
789
 
"@comment regex.h"
790
 
"@comment POSIX.2"
791
 
"@item REG_EBRACK"
792
 
"There were unbalanced square brackets in the regular expression."
793
 
""
794
 
"@comment regex.h"
795
 
"@comment POSIX.2"
796
 
"@item REG_EPAREN"
797
 
"An extended regular expression had unbalanced parentheses,"
798
 
"or a basic regular expression had unbalanced @samp{\\(} and @samp{\\)}."
799
 
""
800
 
"@comment regex.h"
801
 
"@comment POSIX.2"
802
 
"@item REG_EBRACE"
803
 
"The regular expression had unbalanced @samp{\\@{} and @samp{\\@}}."
804
 
""
805
 
"@comment regex.h"
806
 
"@comment POSIX.2"
807
 
"@item REG_ERANGE"
808
 
"One of the endpoints in a range expression was invalid."
809
 
""
810
 
"@comment regex.h"
811
 
"@comment POSIX.2"
812
 
"@item REG_ESPACE"
813
 
"@code{regcomp} ran out of memory."
814
 
"@end table"
815
 
""
816
 
"@node Flags for POSIX Regexps, Matching POSIX Regexps, POSIX Regexp Compilation, Posix Entry Points"
817
 
"@section Flags for POSIX Regular Expressions"
818
 
""
819
 
"These are the bit flags that you can use in the @var{cflags} operand when"
820
 
"compiling a regular expression with @code{regcomp}."
821
 
" "
822
 
"@table @code"
823
 
"@comment regex.h"
824
 
"@comment POSIX.2"
825
 
"@item REG_EXTENDED"
826
 
"Treat the pattern as an extended regular expression, rather than as a"
827
 
"basic regular expression."
828
 
""
829
 
"@comment regex.h"
830
 
"@comment POSIX.2"
831
 
"@item REG_ICASE"
832
 
"Ignore case when matching letters."
833
 
""
834
 
"@comment regex.h"
835
 
"@comment POSIX.2"
836
 
"@item REG_NOSUB"
837
 
"Don't bother storing the contents of the @var{matches-ptr} array."
838
 
""
839
 
"@comment regex.h"
840
 
"@comment POSIX.2"
841
 
"@item REG_NEWLINE"
842
 
"Treat a newline in @var{string} as dividing @var{string} into multiple"
843
 
"lines, so that @samp{$} can match before the newline and @samp{^} can"
844
 
"match after.  Also, don't permit @samp{.} to match a newline, and don't"
845
 
"permit @samp{[^@dots{}]} to match a newline."
846
 
""
847
 
"Otherwise, newline acts like any other ordinary character."
848
 
"@end table"
849
 
""
850
 
"@node Matching POSIX Regexps, Regexp Subexpressions, Flags for POSIX Regexps, Posix Entry Points"
851
 
"@section Matching a Compiled POSIX Regular Expression"
852
 
""
853
 
"Once you have compiled a regular expression, as described in @ref{POSIX"
854
 
"Regexp Compilation}, you can match it against strings using"
855
 
"@code{regexec}.  A match anywhere inside the string counts as success,"
856
 
"unless the regular expression contains anchor characters (@samp{^} or"
857
 
"@samp{$})."
858
 
""
859
 
"@comment regex.h"
860
 
"@comment POSIX.2"
861
 
"@deftypefun int regexec (regex_t *@var{compiled}, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
862
 
"@deftypefunx int regnexec (regex_t *@var{compiled}, int len, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
863
 
"This function tries to match the compiled regular expression"
864
 
"@code{*@var{compiled}} against @var{string}."
865
 
""
866
 
"@code{regexec} returns @code{0} if the regular expression matches;"
867
 
"otherwise, it returns a nonzero value.  See the table below for"
868
 
"what nonzero values mean.  You can use @code{regerror} to produce an"
869
 
"error message string describing the reason for a nonzero value; "
870
 
"see @ref{Regexp Cleanup}."
871
 
""
872
 
"The parameter @var{string} points to the text to search.  When using"
873
 
"@code{regexec}, @var{string} must be 0-terminated.  When using"
874
 
"@code{regnexec}, @var{string} must be @var{len} characters long."
875
 
""
876
 
"@code{regnexec} is not a standard function;  strictly POSIX programs"
877
 
"should avoid using it."
878
 
""
879
 
"The argument @var{eflags} is a word of bit flags that enable various"
880
 
"options."
881
 
""
882
 
"If you want to get information about what part of @var{string} actually"
883
 
"matched the regular expression or its subexpressions, use the arguments"
884
 
"@var{matchptr} and @var{nmatch}.  Otherwise, pass @code{0} for "
885
 
"@var{nmatch}, and @code{NULL} for @var{matchptr}.  @xref{Regexp"
886
 
"Subexpressions}."
887
 
"@end deftypefun"
888
 
""
889
 
"You must match the regular expression with the same set of current"
890
 
"locales that were in effect when you compiled the regular expression."
891
 
""
892
 
"The function @code{regexec} accepts the following flags in the"
893
 
"@var{eflags} argument:"
894
 
""
895
 
"@table @code "
896
 
"@comment regex.h"
897
 
"@comment POSIX.2"
898
 
"@item REG_NOTBOL"
899
 
"Do not regard the beginning of the specified string as the beginning of"
900
 
"a line; more generally, don't make any assumptions about what text might"
901
 
"precede it."
902
 
""
903
 
"@comment regex.h"
904
 
"@comment POSIX.2"
905
 
"@item REG_NOTEOL"
906
 
"Do not regard the end of the specified string as the end of a line; more"
907
 
"generally, don't make any assumptions about what text might follow it."
908
 
"@end table"
909
 
""
910
 
"Here are the possible nonzero values that @code{regexec} can return:"
911
 
""
912
 
"@table @code"
913
 
"@comment regex.h"
914
 
"@comment POSIX.2"
915
 
"@item REG_NOMATCH"
916
 
"The pattern didn't match the string.  This isn't really an error."
917
 
""
918
 
"@comment regex.h"
919
 
"@comment POSIX.2"
920
 
"@item REG_ESPACE"
921
 
"@code{regexec} ran out of memory."
922
 
"@end table"
923
 
""
924
 
"@node Regexp Subexpressions, Subexpression Complications, Matching POSIX Regexps, Posix Entry Points"
925
 
"@section Match Results with Subexpressions"
926
 
""
927
 
"When @code{regexec} matches parenthetical subexpressions of"
928
 
"@var{pattern}, it records which parts of @var{string} they match.  It"
929
 
"returns that information by storing the offsets into an array whose"
930
 
"elements are structures of type @code{regmatch_t}.  The first element of"
931
 
"the array (index @code{0}) records the part of the string that matched"
932
 
"the entire regular expression.  Each other element of the array records"
933
 
"the beginning and end of the part that matched a single parenthetical"
934
 
"subexpression."
935
 
""
936
 
"@comment regex.h"
937
 
"@comment POSIX.2"
938
 
"@deftp {Data Type} regmatch_t"
939
 
"This is the data type of the @var{matcharray} array that you pass to"
940
 
"@code{regexec}.  It containes two structure fields, as follows:"
941
 
""
942
 
"@table @code"
943
 
"@item rm_so"
944
 
"The offset in @var{string} of the beginning of a substring.  Add this"
945
 
"value to @var{string} to get the address of that part."
946
 
""
947
 
"@item rm_eo"
948
 
"The offset in @var{string} of the end of the substring."
949
 
"@end table"
950
 
"@end deftp"
951
 
""
952
 
"@comment regex.h"
953
 
"@comment POSIX.2"
954
 
"@deftp {Data Type} regoff_t"
955
 
"@code{regoff_t} is an alias for another signed integer type."
956
 
"The fields of @code{regmatch_t} have type @code{regoff_t}."
957
 
"@end deftp"
958
 
""
959
 
"The @code{regmatch_t} elements correspond to subexpressions"
960
 
"positionally; the first element (index @code{1}) records where the first"
961
 
"subexpression matched, the second element records the second"
962
 
"subexpression, and so on.  The order of the subexpressions is the order"
963
 
"in which they begin."
964
 
""
965
 
"When you call @code{regexec}, you specify how long the @var{matchptr}"
966
 
"array is, with the @var{nmatch} argument.  This tells @code{regexec} how"
967
 
"many elements to store.  If the actual regular expression has more than"
968
 
"@var{nmatch} subexpressions, then you won't get offset information about"
969
 
"the rest of them.  But this doesn't alter whether the pattern matches a"
970
 
"particular string or not."
971
 
""
972
 
"If you don't want @code{regexec} to return any information about where"
973
 
"the subexpressions matched, you can either supply @code{0} for"
974
 
"@var{nmatch}, or use the flag @code{REG_NOSUB} when you compile the"
975
 
"pattern with @code{regcomp}."
976
 
""
977
 
"@node Subexpression Complications, Regexp Cleanup, Regexp Subexpressions, Posix Entry Points"
978
 
"@section Complications in Subexpression Matching"
979
 
""
980
 
"Sometimes a subexpression matches a substring of no characters.  This"
981
 
"happens when @samp{f\\(o*\\)} matches the string @samp{fum}.  (It really"
982
 
"matches just the @samp{f}.)  In this case, both of the offsets identify"
983
 
"the point in the string where the null substring was found.  In this"
984
 
"example, the offsets are both @code{1}."
985
 
""
986
 
"Sometimes the entire regular expression can match without using some of"
987
 
"its subexpressions at all---for example, when @samp{ba\\(na\\)*} matches the"
988
 
"string @samp{ba}, the parenthetical subexpression is not used.  When"
989
 
"this happens, @code{regexec} stores @code{-1} in both fields of the"
990
 
"element for that subexpression."
991
 
""
992
 
"Sometimes matching the entire regular expression can match a particular"
993
 
"subexpression more than once---for example, when @samp{ba\\(na\\)*}"
994
 
"matches the string @samp{bananana}, the parenthetical subexpression"
995
 
"matches three times.  When this happens, @code{regexec} usually stores"
996
 
"the offsets of the last part of the string that matched the"
997
 
"subexpression.  In the case of @samp{bananana}, these offsets are"
998
 
"@code{6} and @code{8}."
999
 
""
1000
 
"But the last match is not always the one that is chosen.  It's more"
1001
 
"accurate to say that the last @emph{opportunity} to match is the one"
1002
 
"that takes precedence.  What this means is that when one subexpression"
1003
 
"appears within another, then the results reported for the inner"
1004
 
"subexpression reflect whatever happened on the last match of the outer"
1005
 
"subexpression.  For an example, consider @samp{\\(ba\\(na\\)*s \\)*} matching"
1006
 
"the string @samp{bananas bas }.  The last time the inner expression"
1007
 
"actually matches is near the end of the first word.  But it is "
1008
 
"@emph{considered} again in the second word, and fails to match there."
1009
 
"@code{regexec} reports nonuse of the ``na'' subexpression."
1010
 
""
1011
 
"Another place where this rule applies is when the regular expression"
1012
 
"@w{@samp{\\(ba\\(na\\)*s \\|nefer\\(ti\\)* \\)*}} matches @samp{bananas nefertiti}."
1013
 
"The ``na'' subexpression does match in the first word, but it doesn't"
1014
 
"match in the second word because the other alternative is used there."
1015
 
"Once again, the second repetition of the outer subexpression overrides"
1016
 
"the first, and within that second repetition, the ``na'' subexpression"
1017
 
"is not used.  So @code{regexec} reports nonuse of the ``na''"
1018
 
"subexpression."
1019
 
""
1020
 
"@node Regexp Cleanup,  , Subexpression Complications, Posix Entry Points"
1021
 
"@section POSIX Regexp Matching Cleanup"
1022
 
""
1023
 
"When you are finished using a compiled regular expression, you can"
1024
 
"free the storage it uses by calling @code{regfree}."
1025
 
""
1026
 
"@comment regex.h"
1027
 
"@comment POSIX.2"
1028
 
"@deftypefun void regfree (regex_t *@var{compiled})"
1029
 
"Calling @code{regfree} frees all the storage that @code{*@var{compiled}}"
1030
 
"points to.  This includes various internal fields of the @code{regex_t}"
1031
 
"structure that aren't documented in this manual."
1032
 
""
1033
 
"@code{regfree} does not free the object @code{*@var{compiled}} itself."
1034
 
"@end deftypefun"
1035
 
""
1036
 
"You should always free the space in a @code{regex_t} structure with"
1037
 
"@code{regfree} before using the structure to compile another regular"
1038
 
"expression."
1039
 
""
1040
 
"When @code{regcomp} or @code{regexec} reports an error, you can use"
1041
 
"the function @code{regerror} to turn it into an error message string."
1042
 
""
1043
 
"@comment regex.h"
1044
 
"@comment POSIX.2"
1045
 
"@deftypefun size_t regerror (int @var{errcode}, regex_t *@var{compiled}, char *@var{buffer}, size_t @var{length})"
1046
 
"This function produces an error message string for the error code"
1047
 
"@var{errcode}, and stores the string in @var{length} bytes of memory"
1048
 
"starting at @var{buffer}.  For the @var{compiled} argument, supply the"
1049
 
"same compiled regular expression structure that @code{regcomp} or"
1050
 
"@code{regexec} was working with when it got the error.  Alternatively,"
1051
 
"you can supply @code{NULL} for @var{compiled}; you will still get a"
1052
 
"meaningful error message, but it might not be as detailed."
1053
 
""
1054
 
"If the error message can't fit in @var{length} bytes (including a"
1055
 
"terminating null character), then @code{regerror} truncates it."
1056
 
"The string that @code{regerror} stores is always null-terminated"
1057
 
"even if it has been truncated."
1058
 
""
1059
 
"The return value of @code{regerror} is the minimum length needed to"
1060
 
"store the entire error message.  If this is less than @var{length}, then"
1061
 
"the error message was not truncated, and you can use it.  Otherwise, you"
1062
 
"should call @code{regerror} again with a larger buffer."
1063
 
""
1064
 
"Here is a function which uses @code{regerror}, but always dynamically"
1065
 
"allocates a buffer for the error message:"
1066
 
""
1067
 
"@smallexample"
1068
 
"char *get_regerror (int errcode, regex_t *compiled)"
1069
 
"@{"
1070
 
"  size_t length = regerror (errcode, compiled, NULL, 0);"
1071
 
"  char *buffer = xmalloc (length);"
1072
 
"  (void) regerror (errcode, compiled, buffer, length);"
1073
 
"  return buffer;"
1074
 
"@}"
1075
 
"@end smallexample"
1076
 
"@end deftypefun"
1077
 
""
1078
 
"@node  Beyond POSIX, Rx Theory, Posix Entry Points, Top"
1079
 
"@chapter Beyond POSIX"
1080
 
""
1081
 
"This section is not finished documentation, but rather a collection of"
1082
 
"pointers towards some of the interesting, non-standard features of Rx."
1083
 
""
1084
 
"@section New Regexp Operators"
1085
 
""
1086
 
"Rx supports some unusual regexp syntax."
1087
 
""
1088
 
"@code{[[:cut N:]]} sets @code{pmatch[0].final_tag} to N and causes the"
1089
 
"matching to stop instantly.  If N is 0, the overall match fails,"
1090
 
"otherwise it succeeds."
1091
 
""
1092
 
"@code{[[:(:]] ... [[:):]]} is just like @code{\\( ... \\)} except that in"
1093
 
"the first case, no pmatch entries are changed, and the subexpression is"
1094
 
"not counted in the numbering of parenthesized subexpressions."
1095
 
""
1096
 
"@code{[[:(:]] ... [[:):]]} can be used when you do not need to know"
1097
 
"where a subexpression matched but are only using parentheses to effect"
1098
 
"the parsing of the regexp.  "
1099
 
""
1100
 
"There are two reasons to use @code{[[:(:]] ... [[:):]]}:"
1101
 
""
1102
 
"1. regexec will run faster."
1103
 
""
1104
 
"2. Currently, only 8 backreferencable subexpressions are supported:"
1105
 
"@code{\\1 .. \\9}.  Using @code{[[:(:]] ... [[:):]]} is a way to conserve"
1106
 
"backreferencable subexpression names in an expression with many"
1107
 
"parentheses."
1108
 
""
1109
 
"@section New POSIX Functions"
1110
 
""
1111
 
"@code{regncomp} and @code{regnexec} are non-standard generalizations of"
1112
 
"@code{regcomp} and @code{regexec}."
1113
 
""
1114
 
"@section Tuning POSIX performance"
1115
 
""
1116
 
"Two mysterious parmaters can be used to trade-off performance and"
1117
 
"memory use."
1118
 
""
1119
 
"At compile-time they are @code{RX_DEFAULT_DFA_CACHE_SIZE} and"
1120
 
"@code{RX_DEFAULT_NFA_DELAY}.  "
1121
 
""
1122
 
"If you want to mess with these (I generally don't advise it), I suggest"
1123
 
"experimenting for your particular application/memory situation; frob"
1124
 
"these by powers of two and try out the results on what you expect will"
1125
 
"be typical regexp workloads."
1126
 
""
1127
 
"You can also set those parameters at run-time (before calling any regexp"
1128
 
"functions) by tweaking the corresponding variables:"
1129
 
""
1130
 
"@code{rx_default_cache->bytes_allowed}"
1131
 
""
1132
 
"and"
1133
 
""
1134
 
"@code{rx_basic_unfaniverse_delay}"
1135
 
""
1136
 
""
1137
 
""
1138
 
"@section POSIX stream-style interface"
1139
 
""
1140
 
"@code{rx_make_solutions}, @code{rx_next_solution}, and"
1141
 
"@code{rx_free_solutions} are a lower level alternative to the posix"
1142
 
"functions.  Using those functions, you can compare a compiled regexp to"
1143
 
"a string that is not contiguous in memory or even a string that is not"
1144
 
"entirely in memory at any one time."
1145
 
""
1146
 
"The code in rxposix.c points out how those functions are used."
1147
 
""
1148
 
""
1149
 
"@section DFAs Directly"
1150
 
""
1151
 
"If you are only interested in pure regular expressions (no pmatch data,"
1152
 
"no backreferences, and no counted subexpressions), you can parse a"
1153
 
"regexp using @code{rx_parse}, convert it to an nfa using @code{rx_unfa},"
1154
 
"and run the dfa using @code{rx_init_system}, @code{rx_advance_to_final},"
1155
 
"and @code{rx_terminate_system}.  The dfa Scheme primitives in"
1156
 
"@file{rgx.c} may provide some guide."
1157
 
""
1158
 
"@node    Rx Theory,  , Beyond POSIX, Top"
1159
 
"@chapter Rx Theory"
1160
 
"@cindex Rx Theory"
1161
 
""
1162
 
"There are two match algorithms.  One is for truly regular regexps (those"
1163
 
"that can be reduced to a dfa).  The other is for non-regular regexps."
1164
 
""
1165
 
"The dfa algorithm implements the idea suggested in @cite{Compilers} by"
1166
 
"Aho, Sethi and Ullman:"
1167
 
""
1168
 
"@quotation"
1169
 
"[One] approach [to pattern matching regular expressions] is to use a"
1170
 
"DFA, but avoid constructing all of the transition table by using a"
1171
 
"technique called \"lazy transition evaluation\".  Here, transitions are"
1172
 
"computed at run time [when] actually needed. [T]ransitions are"
1173
 
"stored in a cache. [....] If the cache becomes full, we can erase some"
1174
 
"previously computed transition to make room for the new transition."
1175
 
"@end quotation"
1176
 
""
1177
 
"The implementation in Rx is generalized from that, but the above"
1178
 
"description covers what is used for Posix patterns. "
1179
 
""
1180
 
"The non-dfa algorithm implements a \"recursive decomposition\" technique"
1181
 
"described in email by Henry Spencer.  For a given pattern, this"
1182
 
"algorithm first checks to see if a simpler, superset language,"
1183
 
"DFA-pattern matches.  If it does, then this algorithm does the"
1184
 
"detail-work to see if the non-DFA pattern matches."
1185
 
"\\input texinfo   @c -*-texinfo-*-"
1186
 
"@c %**start of header"
1187
 
"@setfilename rx.info"
1188
 
"@settitle Rx"
1189
 
"@setchapternewpage odd"
1190
 
"@c %**end of header"
1191
 
""
1192
 
"@c  This title page illustrates only one of the"
1193
 
"@c  two methods of forming a title page."
1194
 
""
1195
 
"@titlepage"
1196
 
"@title Rx"
1197
 
"@author Tom Lord"
1198
 
""
1199
 
"@center except the chapter \"Posix Entry Points\" "
1200
 
"@center from @emph{The GNU C Library} reference manual"
1201
 
"@center by Sandra Loosemore"
1202
 
"@center with"
1203
 
"@center Richard M. Stallman, Roland McGrath, and Andrew Oram"
1204
 
""
1205
 
"@c  The following two commands"
1206
 
"@c  start the copyright page."
1207
 
"@page"
1208
 
"@vskip 0pt plus 1filll"
1209
 
"Copyright @copyright{} 1995 Cygnus Support"
1210
 
""
1211
 
"except the chapter \"Posix Entry Points\" which is:"
1212
 
""
1213
 
"Copyright @copyright{} 1995 Free Software Foundation, Inc."
1214
 
""
1215
 
"Permission is granted to make and distribute verbatim copies of"
1216
 
"this manual provided the copyright notice and this permission notice"
1217
 
"are preserved on all copies."
1218
 
""
1219
 
"Permission is granted to copy and distribute modified versions of this"
1220
 
"manual under the conditions for verbatim copying, provided that the entire"
1221
 
"resulting derived work is distributed under the terms of a permission"
1222
 
"notice identical to this one."
1223
 
""
1224
 
"Permission is granted to copy and distribute translations of this manual"
1225
 
"into another language, under the above conditions for modified versions,"
1226
 
"except that this permission notice may be stated in a translation approved"
1227
 
"by the author."
1228
 
""
1229
 
"@end titlepage"
1230
 
""
1231
 
"@node Top, Copying, (dir), (dir)"
1232
 
"@top Rx"
1233
 
""
1234
 
"This document describes Rx."
1235
 
""
1236
 
"@menu"
1237
 
"* Copying::                            Sharing is good."
1238
 
"* Overview::                           Fnord"
1239
 
"* Posix Basic Regular Expressions::    A popular regexp syntax."
1240
 
"* Posix Entry Points::                 The POSIX way to regexp."
1241
 
"* Beyond POSIX::                       Hints about cool features."
1242
 
"* Rx Theory::                          Hints about how it works."
1243
 
"@end menu"
1244
 
""
1245
 
"@node Copying, Overview, Top, Top"
1246
 
"@chapter Copying"
1247
 
""
1248
 
"@center Copyright (C) 1996"
1249
 
"@center Tom Lord"
1250
 
"@center Berkeley, CA USA"
1251
 
""
1252
 
"@center except portions of \"POSIX Regex Functions\" which are"
1253
 
"@center Copyright (C) 1995"
1254
 
"@center Free Software Foundation, Inc."
1255
 
""
1256
 
"@noindent"
1257
 
"Permission to use, copy, modify, distribute, and sell this software and"
1258
 
"its documentation for any purpose is hereby granted without fee,"
1259
 
"provided that the above copyright notice appear in all copies and that"
1260
 
"both that copyright notice and this permission notice appear in"
1261
 
"supporting documentation."
1262
 
""
1263
 
"@center NO WARRANTY"
1264
 
""
1265
 
"@noindent"
1266
 
"BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR"
1267
 
"THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN"
1268
 
"OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES"
1269
 
"PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER"
1270
 
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED"
1271
 
"WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE"
1272
 
"ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH"
1273
 
"YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL"
1274
 
"NECESSARY SERVICING, REPAIR OR CORRECTION."
1275
 
""
1276
 
"@noindent"
1277
 
"IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING"
1278
 
"WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR"
1279
 
"REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR"
1280
 
"DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL"
1281
 
"DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM"
1282
 
"(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED"
1283
 
"INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF"
1284
 
"THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR"
1285
 
"OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES."
1286
 
""
1287
 
"@node  Overview, Posix Basic Regular Expressions, Copying, Top"
1288
 
"@chapter Overview"
1289
 
""
1290
 
"Nothing to say here, yet."
1291
 
""
1292
 
"@node  Posix Basic Regular Expressions, Posix Entry Points, Overview, Top"
1293
 
"@chapter Posix Basic Regular Expressions"
1294
 
""
1295
 
"The Posix Basic Regular Expression language is a notation for describing"
1296
 
"textual patterns.  Regexps are typically used by comparing them to a"
1297
 
"string to see if that string matches the pattern, or by searching within"
1298
 
"a string for a substring that matches."
1299
 
""
1300
 
"This chapter introduces the Posix regexp notation.  This is not a formal"
1301
 
"or precise definition of Posix regexps -- it is an intuitive and"
1302
 
"hopefully expository description of them."
1303
 
""
1304
 
"@menu"
1305
 
"* An Introduction to Regexps::  "
1306
 
"* Literal Regexps::             "
1307
 
"* Character Sets::              "
1308
 
"* Subexpressions::              "
1309
 
"* Repeated Subexpressions::     "
1310
 
"* Optional Subexpressions::     "
1311
 
"* Counted Subexpressions::      "
1312
 
"* Alternative Subexpressions::  "
1313
 
"* Backreferences::              "
1314
 
"* A Summary of Regexp Syntax::  "
1315
 
"* Ambiguous Patterns::          "
1316
 
"@end menu"
1317
 
""
1318
 
""
1319
 
"@node  An Introduction to Regexps, Literal Regexps, Posix Basic Regular Expressions, Posix Basic Regular Expressions"
1320
 
"@section An Introduction to Regexps"
1321
 
""
1322
 
"In the simplest cases, a regexp is just a literal string that must"
1323
 
"match exactly.   For example, the pattern:"
1324
 
""
1325
 
"@example"
1326
 
"regexp"
1327
 
"@end example"
1328
 
""
1329
 
"matches the string \"regexp\" and no others."
1330
 
""
1331
 
"Some characters have a special meaning when they occur in a regexp."
1332
 
"They aren't matched literally as in the previous example, but instead"
1333
 
"denote a more general pattern.   For example, the character @code{*}"
1334
 
"is used to indicate that the preceeding element of a regexp may be"
1335
 
"repeated 0, 1, or more times.  In the pattern:"
1336
 
""
1337
 
"@example"
1338
 
"smooo*th"
1339
 
"@end example"
1340
 
""
1341
 
"the @code{*} indicates that the preceeding @code{o} can be repeated 0 or"
1342
 
"more times.   So the pattern matches:"
1343
 
""
1344
 
"@example"
1345
 
"smooth"
1346
 
"smoooth"
1347
 
"smooooth"
1348
 
"smoooooth"
1349
 
"..."
1350
 
"@end example"
1351
 
""
1352
 
"Suppose you want to write a pattern that literally matches a special"
1353
 
"character like @code{*} -- in other words, you don't want to @code{*} to"
1354
 
"indicate a permissible repetition, but to match @code{*} literally.  This"
1355
 
"is accomplished by quoting the special character with a backslash.  "
1356
 
"The pattern:"
1357
 
""
1358
 
"@example"
1359
 
"smoo\\*th"
1360
 
"@end example"
1361
 
""
1362
 
"matches the string:"
1363
 
""
1364
 
"@example"
1365
 
"smoo*th"
1366
 
"@end example"
1367
 
""
1368
 
""
1369
 
"and no other strings."
1370
 
""
1371
 
"In five cases, the pattern is reversed -- a backslash makes the"
1372
 
"character special instead of making a special character normal.  The"
1373
 
"characters @code{+}, @code{?}, @code{|}, @code{(}, and @code{)} are"
1374
 
"normal but the sequences @code{\\+}, @code{\\?}, @code{\\|}, @code{\\(}, and"
1375
 
"@code{\\)} are special (their meaning is described later)."
1376
 
""
1377
 
"The remaining sections of this chapter introduce and explain the various"
1378
 
"special characters that can occur in regexps."
1379
 
""
1380
 
"@node  Literal Regexps, Character Sets, An Introduction to Regexps, Posix Basic Regular Expressions"
1381
 
"@section Literal Regexps"
1382
 
""
1383
 
"A literal regexp is a string which contains no special characters."
1384
 
"A literal regexp matches an identical string, but no other characters."
1385
 
"For example:"
1386
 
""
1387
 
"@example"
1388
 
"literally"
1389
 
"@end example"
1390
 
""
1391
 
"matches"
1392
 
""
1393
 
"@example"
1394
 
"literally"
1395
 
"@end example"
1396
 
""
1397
 
"and nothing else."
1398
 
""
1399
 
"Generally, whitespace characters, numbers, and letters are not special."
1400
 
"Some punctuation characters are special and some are not (the syntax"
1401
 
"summary at the end of this chapter makes a convenient reference for"
1402
 
"which characters are special and which aren't)."
1403
 
""
1404
 
""
1405
 
"@node  Character Sets, Subexpressions, Literal Regexps, Posix Basic Regular Expressions"
1406
 
"@section Character Sets"
1407
 
""
1408
 
"This section introduces the special characters @code{.} and @code{[}."
1409
 
""
1410
 
"@code{.} matches any character except the NULL character.  For example:"
1411
 
""
1412
 
"@example"
1413
 
"p.ck"
1414
 
"@end example"
1415
 
""
1416
 
"matches"
1417
 
""
1418
 
"@example"
1419
 
"pick"
1420
 
"pack"
1421
 
"puck"
1422
 
"pbck"
1423
 
"pcck"
1424
 
"p.ck"
1425
 
""
1426
 
"..."
1427
 
"@end example"
1428
 
""
1429
 
""
1430
 
"@code{[} begins a @dfn{character set}.  A character set is similar to"
1431
 
"@code{.} in that it matches not a single, literal character, but any"
1432
 
"of a set of characters.   @code{[} is different from @code{.} in that"
1433
 
"with @code{[}, you define the set of characters explicitly."
1434
 
""
1435
 
"There are three basic forms a character set can take."
1436
 
""
1437
 
"In the first form, the character set is spelled out:"
1438
 
""
1439
 
"@example"
1440
 
"[<cset-spec>]  -- every character in <cset-spec> is in the set."
1441
 
"@end example"
1442
 
""
1443
 
"In the second form, the character set indicated is the negation of"
1444
 
"a character set is explicitly spelled out:"
1445
 
""
1446
 
"@example"
1447
 
"[^<cset-spec>] -- every character *not* in <cset-spec> is in the set."
1448
 
"@end example"
1449
 
""
1450
 
"A @code{<cset-spec>} is more or less an explicit enumeration of a set"
1451
 
"of characters.  It can be written as a string of individual characters:"
1452
 
""
1453
 
"@example"
1454
 
"[aeiou]"
1455
 
"@end example"
1456
 
""
1457
 
"or as a range of characters:"
1458
 
""
1459
 
"@example"
1460
 
"[0-9]"
1461
 
"@end example"
1462
 
""
1463
 
"These two forms can be mixed:"
1464
 
""
1465
 
"@example"
1466
 
"[A-za-z0-9_$]"
1467
 
"@end example"
1468
 
""
1469
 
"Note that special regexp characters (such as @code{*}) are @emph{not}"
1470
 
"special within a character set.  @code{-}, as illustrated above,"
1471
 
"@emph{is} special, except, as illustrated below, when it is the first"
1472
 
"character mentioned."
1473
 
""
1474
 
"This is a four-character set:"
1475
 
""
1476
 
"@example"
1477
 
"[-+*/]"
1478
 
"@end example"
1479
 
""
1480
 
"The third form of a character set makes use of a pre-defined \"character"
1481
 
"class\":"
1482
 
""
1483
 
"@example"
1484
 
"[[:class-name:]] -- every character described by class-name is in the set."
1485
 
"@end example"
1486
 
""
1487
 
"The supported character classes are:"
1488
 
""
1489
 
"@example"
1490
 
"alnum  - the set of alpha-numeric characters"
1491
 
"alpha  - the set of alphabetic characters"
1492
 
"blank  - tab and space"
1493
 
"cntrl  - the control characters"
1494
 
"digit  - decimal digits"
1495
 
"graph  - all printable characters except space"
1496
 
"lower  - lower case letters"
1497
 
"print  - the \"printable\" characters"
1498
 
"punct  - punctuation"
1499
 
"space  - whitespace characters"
1500
 
"upper  - upper case letters"
1501
 
"xdigit - hexidecimal digits"
1502
 
"@end example"
1503
 
""
1504
 
"Finally, character class sets can also be inverted:"
1505
 
""
1506
 
"@example"
1507
 
"[^[:space:]] - all non-whitespace characters"
1508
 
"@end example"
1509
 
""
1510
 
"Character sets can be used in a regular expression anywhere a literal"
1511
 
"character can."
1512
 
""
1513
 
"@node  Subexpressions, Repeated Subexpressions, Character Sets, Posix Basic Regular Expressions"
1514
 
"@section Subexpressions"
1515
 
""
1516
 
"A subexpression is a regular expression enclosed in @code{\\(} and"
1517
 
"@code{\\)}.  A subexpression can be used anywhere a single character or"
1518
 
"character set can be used."
1519
 
""
1520
 
"Subexpressions are useful for grouping regexp constructs.  For example,"
1521
 
"the repeat operator, @code{*}, usually applies to just the preceeding"
1522
 
"character.   Recall that:"
1523
 
""
1524
 
"@example"
1525
 
"smooo*th"
1526
 
"@end example"
1527
 
""
1528
 
"matches"
1529
 
""
1530
 
"@example"
1531
 
"smooth"
1532
 
"smoooth"
1533
 
"..."
1534
 
"@end example"
1535
 
""
1536
 
""
1537
 
"Using a subexpression, we can apply @code{*} to a longer string:"
1538
 
""
1539
 
"@example"
1540
 
"banan\\(an\\)*a"
1541
 
"@end example"
1542
 
""
1543
 
"matches"
1544
 
""
1545
 
"@example"
1546
 
"banana"
1547
 
"bananana"
1548
 
"banananana"
1549
 
"..."
1550
 
"@end example"
1551
 
""
1552
 
"Subexpressions also have a special meaning with regard to backreferences"
1553
 
"and substitutions (see @xref{Backreferences})."
1554
 
""
1555
 
""
1556
 
"@node  Repeated Subexpressions, Optional Subexpressions, Subexpressions, Posix Basic Regular Expressions"
1557
 
"@section Repeated Subexpressions"
1558
 
""
1559
 
""
1560
 
"@code{*} is the repeat operator.  It applies to the preceeding"
1561
 
"character, character set, subexpression or backreference.  It indicates"
1562
 
"that the preceeding element can be matched 0 or more times:"
1563
 
""
1564
 
"@example"
1565
 
"bana\\(na\\)*"
1566
 
"@end example"
1567
 
""
1568
 
"matches"
1569
 
""
1570
 
"@example"
1571
 
"bana"
1572
 
"banana"
1573
 
"bananana"
1574
 
"banananana"
1575
 
"..."
1576
 
"@end example"
1577
 
""
1578
 
""
1579
 
""
1580
 
"@code{\\+} is similar to @code{*} except that @code{\\+} requires the"
1581
 
"preceeding element to be matched at least once.  So while:"
1582
 
""
1583
 
"@example"
1584
 
"bana\\(na\\)*"
1585
 
"@end example"
1586
 
""
1587
 
"matches"
1588
 
""
1589
 
"@example"
1590
 
"bana"
1591
 
"@end example"
1592
 
""
1593
 
"@example"
1594
 
"bana(na\\)\\+"
1595
 
"@end example"
1596
 
""
1597
 
"does not.   Both match "
1598
 
""
1599
 
"@example"
1600
 
"banana"
1601
 
"bananana"
1602
 
"banananana"
1603
 
"..."
1604
 
"@end example"
1605
 
""
1606
 
"Thus, @code{bana\\(na\\)+} is short-hand for @code{banana\\(na\\)*}."
1607
 
""
1608
 
""
1609
 
"@node  Optional Subexpressions, Counted Subexpressions, Repeated Subexpressions, Posix Basic Regular Expressions"
1610
 
"@section Optional Subexpressions"
1611
 
""
1612
 
"@code{\\?} indicates that the preceeding character, character set, or"
1613
 
"subexpression is optional.  It is permitted to match, or to be skipped:"
1614
 
""
1615
 
"@example"
1616
 
"CSNY\\?"
1617
 
"@end example"
1618
 
""
1619
 
"matches both"
1620
 
""
1621
 
"@example"
1622
 
"CSN"
1623
 
"@end example"
1624
 
""
1625
 
"and "
1626
 
""
1627
 
"@example"
1628
 
"CSNY"
1629
 
"@end example"
1630
 
""
1631
 
""
1632
 
"@node  Counted Subexpressions, Alternative Subexpressions, Optional Subexpressions, Posix Basic Regular Expressions"
1633
 
"@section Counted Subexpressions"
1634
 
""
1635
 
"An interval expression, @code{@{m,n@}} where @code{m} and @code{n} are"
1636
 
"non-negative integers with @code{n >= m}, applies to the preceeding"
1637
 
"character, character set, subexpression or backreference.  It indicates"
1638
 
"that the preceeding element must match at least @code{m} times and may"
1639
 
"match as many as @code{n} times."
1640
 
""
1641
 
"For example:"
1642
 
""
1643
 
"@example"
1644
 
"c\\([ad]\\)@{1,4@}"
1645
 
"@end example"
1646
 
""
1647
 
"matches"
1648
 
""
1649
 
"@example"
1650
 
"car"
1651
 
"cdr"
1652
 
"caar"
1653
 
"cdar"
1654
 
"..."
1655
 
"caaar"
1656
 
"cdaar"
1657
 
"..."
1658
 
"cadddr"
1659
 
"cddddr"
1660
 
"@end example"
1661
 
""
1662
 
""
1663
 
""
1664
 
"@node  Alternative Subexpressions, Backreferences, Counted Subexpressions, Posix Basic Regular Expressions"
1665
 
"@section Alternative Subexpressions"
1666
 
""
1667
 
"An alternative is written:"
1668
 
""
1669
 
"@example"
1670
 
"regexp-1\\|regexp-2\\|regexp-3\\|..."
1671
 
"@end example"
1672
 
""
1673
 
"It matches anything matched by some @code{regexp-n}.  For example:"
1674
 
""
1675
 
"@example"
1676
 
"Crosby, Stills, \\(and Nash\\|Nash, and Young\\)"
1677
 
"@end example"
1678
 
""
1679
 
"matches"
1680
 
""
1681
 
"@example"
1682
 
"Crosby, Stills, and Nash"
1683
 
"@end example"
1684
 
""
1685
 
"and"
1686
 
""
1687
 
"@example"
1688
 
"Crosby, Stills, Nash, and Young"
1689
 
"@end example"
1690
 
""
1691
 
""
1692
 
"@node  Backreferences, A Summary of Regexp Syntax, Alternative Subexpressions, Posix Basic Regular Expressions"
1693
 
"@section Backreferences, Extractions and Substitutions"
1694
 
""
1695
 
"A backreference is written @code{\\n} where @code{n} is some single digit"
1696
 
"other than 0.  To be a valid backreference, there must be at least"
1697
 
"@code{n} parenthesized subexpressions in the pattern prior to the"
1698
 
"backreference."
1699
 
""
1700
 
"A backreference matches a literal copy of whatever was matched by the"
1701
 
"corresponding subexpression.  For example,"
1702
 
""
1703
 
"@example"
1704
 
"\\(.*\\)-\\1"
1705
 
"@end example"
1706
 
""
1707
 
"matches:"
1708
 
""
1709
 
"@example"
1710
 
"go-go"
1711
 
"ha-ha"
1712
 
"wakka-wakka"
1713
 
"..."
1714
 
"@end example"
1715
 
""
1716
 
"In some applications, subexpressions are used to extract substrings."
1717
 
"For example, Emacs has the functions @code{match-beginnning} and"
1718
 
"@code{match-end} which report the positions of strings matched by"
1719
 
"subexpressions.  These functions use the same numbering scheme for"
1720
 
"subexpressions as backreferences, with the additional rule that"
1721
 
"subexpression 0 is defined to be the whole regexp."
1722
 
""
1723
 
"In some applications, subexpressions are used in string substitution."
1724
 
"This again uses the backreference numbering scheme. For example, this sed"
1725
 
"command:"
1726
 
""
1727
 
"@example"
1728
 
"s/From:.*<\\(.*\\)>/To: \\1/"
1729
 
"@end example"
1730
 
""
1731
 
"first matches the line:"
1732
 
""
1733
 
"@example"
1734
 
"From: Joe Schmoe <schmoe@@uspringfield.edu>"
1735
 
"@end example"
1736
 
""
1737
 
"when it does, subexpression 1 matches \"schmoe@@uspringfield.edu\"."
1738
 
"The command replaces the matched line with \"To: \\1\" after doing"
1739
 
"subexpression substitution on it to get:"
1740
 
""
1741
 
"@example"
1742
 
"To: schmoe@@uspringfield.edu"
1743
 
"@end example"
1744
 
""
1745
 
""
1746
 
"@node  A Summary of Regexp Syntax, Ambiguous Patterns, Backreferences, Posix Basic Regular Expressions"
1747
 
"@section A Summary of Regexp Syntax"
1748
 
""
1749
 
"In summary, regexps can be:"
1750
 
""
1751
 
"@code{abcd} -- matching a string literally"
1752
 
""
1753
 
"@code{.} -- matching everything except NULL"
1754
 
""
1755
 
"@code{[a-z_?]}, @code{^[a-z_?]}, @code{[[:alpha:]]} and"
1756
 
"@code{[^[:alpha:]]} -- matching character sets"
1757
 
""
1758
 
"@code{\\(subexp\\)} -- grouping an expression into a subexpression."
1759
 
""
1760
 
"@code{\\n} -- match a copy of whatever was matched by the nth subexpression."
1761
 
""
1762
 
"The following special characters and sequences can be applied to a"
1763
 
"character, character set, subexpression, or backreference:"
1764
 
""
1765
 
"@code{*} -- repeat the preceeding element 0 or more times."
1766
 
""
1767
 
"@code{\\+} -- repeat the preceeding element 1 or more times."
1768
 
""
1769
 
"@code{\\?} -- match the preceeding element 0 or 1 time."
1770
 
""
1771
 
"@code{@{m,n@}} -- match the preceeding element at least @code{m}, and as"
1772
 
"many as @code{n} times."
1773
 
""
1774
 
"@code{regexp-1\\|regexp-2\\|..} -- match any regexp-n."
1775
 
""
1776
 
"A special character, like @code{.} or @code{*} can be made into a literal"
1777
 
"character by prefixing it with @code{\\}."
1778
 
""
1779
 
"A special sequence, like @code{\\+} or @code{\\?} can be made into a"
1780
 
"literal character by dropping the @code{\\}."
1781
 
""
1782
 
""
1783
 
"@node  Ambiguous Patterns,  , A Summary of Regexp Syntax, Posix Basic Regular Expressions"
1784
 
"@section Ambiguous Patterns"
1785
 
""
1786
 
"Sometimes a regular expression appears to be ambiguous.  For example, suppose"
1787
 
"we compare the pattern:"
1788
 
""
1789
 
"@example"
1790
 
"begin\\|beginning"
1791
 
"@end example"
1792
 
""
1793
 
"to the string"
1794
 
""
1795
 
"@example"
1796
 
"beginning"
1797
 
"@end example"
1798
 
""
1799
 
"either just the first 5 characters will match, or the whole string will match."
1800
 
""
1801
 
"In every case like this, the longer match is preferred.  The whole"
1802
 
"string will match."
1803
 
""
1804
 
"Sometimes there is ambiguity not about how many characters to match, but"
1805
 
"where the subexpressions occur within the match.  This can effect"
1806
 
"extraction functions like Emacs' @code{match-beginning} or rewrite"
1807
 
"functions like sed's @code{s} command.  For example, consider matching"
1808
 
"the pattern:"
1809
 
""
1810
 
"@example"
1811
 
"b\\(\\[^q]*\\)\\(ing\\)?"
1812
 
"@end example"
1813
 
""
1814
 
"against the string"
1815
 
""
1816
 
"@example"
1817
 
"beginning"
1818
 
"@end example"
1819
 
""
1820
 
"One possibility is that the first subexpression matches \"eginning\" and"
1821
 
"the second is skipped.  Another possibility is that the first"
1822
 
"subexpression matches \"eginn\" and the second matches \"ing\"."
1823
 
""
1824
 
"The rule is that consistant with matching as many characters as "
1825
 
"possible, the length of lower numbered subexpressions is maximized"
1826
 
"in preference to maximizing the length of later subexpressions."
1827
 
""
1828
 
"In the case of the above example, the two possible matches are equal in"
1829
 
"overall length.  Therefore, it comes down to maximizing the"
1830
 
"lower-numbered subexpression, \\1.  The correct answer is that \\1 matches"
1831
 
"\"eginning\" and \\2 is skipped."
1832
 
""
1833
 
"@node Posix Entry Points, Beyond POSIX, Posix Basic Regular Expressions, Top"
1834
 
"@chapter Posix Entry Points"
1835
 
""
1836
 
"This section is excerpted from @emph{The GNU C Library} reference manual"
1837
 
"by Sandra Loosemore with Richard M. Stallman, Roland McGrath, and Andrew"
1838
 
"Oram."
1839
 
""
1840
 
"The GNU C library supports the standard POSIX.2 interface.  Programs"
1841
 
"using this interface should include the header file @file{rxposix.h}."
1842
 
""
1843
 
"@menu"
1844
 
"* POSIX Regexp Compilation::    Using @code{regcomp} to prepare to match."
1845
 
"* Flags for POSIX Regexps::     Syntax variations for @code{regcomp}."
1846
 
"* Matching POSIX Regexps::      Using @code{regexec} to match the compiled"
1847
 
"                                  pattern that you get from @code{regcomp}."
1848
 
"* Regexp Subexpressions::       Finding which parts of the string were matched."
1849
 
"* Subexpression Complications::  Find points of which parts were matched."
1850
 
"* Regexp Cleanup::              Freeing storage; reporting errors."
1851
 
"@end menu"
1852
 
""
1853
 
"@node POSIX Regexp Compilation, Flags for POSIX Regexps, Posix Entry Points, Posix Entry Points"
1854
 
"@section POSIX Regular Expression Compilation"
1855
 
""
1856
 
"Before you can actually match a regular expression, you must"
1857
 
"@dfn{compile} it.  This is not true compilation---it produces a special"
1858
 
"data structure, not machine instructions.  But it is like ordinary"
1859
 
"compilation in that its purpose is to enable you to ``execute'' the"
1860
 
"pattern fast.  (@xref{Matching POSIX Regexps}, for how to use the"
1861
 
"compiled regular expression for matching.)"
1862
 
""
1863
 
"There is a special data type for compiled regular expressions:"
1864
 
""
1865
 
"@comment regex.h"
1866
 
"@comment POSIX.2"
1867
 
"@deftp {Data Type} regex_t"
1868
 
"This type of object holds a compiled regular expression."
1869
 
"It is actually a structure.  It has just one field that your programs"
1870
 
"should look at:"
1871
 
""
1872
 
"@table @code"
1873
 
"@item re_nsub"
1874
 
"This field holds the number of parenthetical subexpressions in the"
1875
 
"regular expression that was compiled."
1876
 
"@end table"
1877
 
""
1878
 
"There are several other fields, but we don't describe them here, because"
1879
 
"only the functions in the library should use them."
1880
 
"@end deftp"
1881
 
""
1882
 
"After you create a @code{regex_t} object, you can compile a regular"
1883
 
"expression into it by calling @code{regcomp}."
1884
 
""
1885
 
"@comment regex.h"
1886
 
"@comment POSIX.2"
1887
 
"@deftypefun int regcomp (regex_t *@var{compiled}, const char *@var{pattern}, int @var{cflags})"
1888
 
"@deftypefunx int regncomp (regex_t *@var{compiled}, int len, const char *@var{pattern}, int @var{cflags})"
1889
 
"The function @code{regcomp} ``compiles'' a regular expression into a"
1890
 
"data structure that you can use with @code{regexec} to match against a"
1891
 
"string.  The compiled regular expression format is designed for"
1892
 
"efficient matching.  @code{regcomp} stores it into @code{*@var{compiled}}."
1893
 
""
1894
 
"The parameter @var{pattern} points to the regular expression to be"
1895
 
"compiled.  When using @code{regcomp}, @var{pattern} must be"
1896
 
"0-terminated.  When using @code{regncomp}, @var{pattern} must be"
1897
 
"@var{len} characters long."
1898
 
""
1899
 
"@code{regncomp} is not a standard function;  strictly POSIX programs"
1900
 
"should avoid using it."
1901
 
""
1902
 
"It's up to you to allocate an object of type @code{regex_t} and pass its"
1903
 
"address to @code{regcomp}."
1904
 
""
1905
 
"The argument @var{cflags} lets you specify various options that control"
1906
 
"the syntax and semantics of regular expressions.  @xref{Flags for POSIX"
1907
 
"Regexps}."
1908
 
""
1909
 
"If you use the flag @code{REG_NOSUB}, then @code{regcomp} omits from"
1910
 
"the compiled regular expression the information necessary to record"
1911
 
"how subexpressions actually match.  In this case, you might as well"
1912
 
"pass @code{0} for the @var{matchptr} and @var{nmatch} arguments when"
1913
 
"you call @code{regexec}."
1914
 
""
1915
 
"If you don't use @code{REG_NOSUB}, then the compiled regular expression"
1916
 
"does have the capacity to record how subexpressions match.  Also,"
1917
 
"@code{regcomp} tells you how many subexpressions @var{pattern} has, by"
1918
 
"storing the number in @code{@var{compiled}->re_nsub}.  You can use that"
1919
 
"value to decide how long an array to allocate to hold information about"
1920
 
"subexpression matches."
1921
 
""
1922
 
"@code{regcomp} returns @code{0} if it succeeds in compiling the regular"
1923
 
"expression; otherwise, it returns a nonzero error code (see the table"
1924
 
"below).  You can use @code{regerror} to produce an error message string"
1925
 
"describing the reason for a nonzero value; see @ref{Regexp Cleanup}."
1926
 
""
1927
 
"@end deftypefun"
1928
 
""
1929
 
"Here are the possible nonzero values that @code{regcomp} can return:"
1930
 
""
1931
 
"@table @code"
1932
 
"@comment regex.h"
1933
 
"@comment POSIX.2"
1934
 
"@item REG_BADBR"
1935
 
"There was an invalid @samp{\\@{@dots{}\\@}} construct in the regular"
1936
 
"expression.  A valid @samp{\\@{@dots{}\\@}} construct must contain either"
1937
 
"a single number, or two numbers in increasing order separated by a"
1938
 
"comma."
1939
 
""
1940
 
"@comment regex.h"
1941
 
"@comment POSIX.2"
1942
 
"@item REG_BADPAT"
1943
 
"There was a syntax error in the regular expression."
1944
 
""
1945
 
"@comment regex.h"
1946
 
"@comment POSIX.2"
1947
 
"@item REG_BADRPT"
1948
 
"A repetition operator such as @samp{?} or @samp{*} appeared in a bad"
1949
 
"position (with no preceding subexpression to act on)."
1950
 
""
1951
 
"@comment regex.h"
1952
 
"@comment POSIX.2"
1953
 
"@item REG_ECOLLATE"
1954
 
"The regular expression referred to an invalid collating element (one not"
1955
 
"defined in the current locale for string collation).  "
1956
 
""
1957
 
"@comment regex.h"
1958
 
"@comment POSIX.2"
1959
 
"@item REG_ECTYPE"
1960
 
"The regular expression referred to an invalid character class name."
1961
 
""
1962
 
"@comment regex.h"
1963
 
"@comment POSIX.2"
1964
 
"@item REG_EESCAPE"
1965
 
"The regular expression ended with @samp{\\}."
1966
 
""
1967
 
"@comment regex.h"
1968
 
"@comment POSIX.2"
1969
 
"@item REG_ESUBREG"
1970
 
"There was an invalid number in the @samp{\\@var{digit}} construct."
1971
 
""
1972
 
"@comment regex.h"
1973
 
"@comment POSIX.2"
1974
 
"@item REG_EBRACK"
1975
 
"There were unbalanced square brackets in the regular expression."
1976
 
""
1977
 
"@comment regex.h"
1978
 
"@comment POSIX.2"
1979
 
"@item REG_EPAREN"
1980
 
"An extended regular expression had unbalanced parentheses,"
1981
 
"or a basic regular expression had unbalanced @samp{\\(} and @samp{\\)}."
1982
 
""
1983
 
"@comment regex.h"
1984
 
"@comment POSIX.2"
1985
 
"@item REG_EBRACE"
1986
 
"The regular expression had unbalanced @samp{\\@{} and @samp{\\@}}."
1987
 
""
1988
 
"@comment regex.h"
1989
 
"@comment POSIX.2"
1990
 
"@item REG_ERANGE"
1991
 
"One of the endpoints in a range expression was invalid."
1992
 
""
1993
 
"@comment regex.h"
1994
 
"@comment POSIX.2"
1995
 
"@item REG_ESPACE"
1996
 
"@code{regcomp} ran out of memory."
1997
 
"@end table"
1998
 
""
1999
 
"@node Flags for POSIX Regexps, Matching POSIX Regexps, POSIX Regexp Compilation, Posix Entry Points"
2000
 
"@section Flags for POSIX Regular Expressions"
2001
 
""
2002
 
"These are the bit flags that you can use in the @var{cflags} operand when"
2003
 
"compiling a regular expression with @code{regcomp}."
2004
 
" "
2005
 
"@table @code"
2006
 
"@comment regex.h"
2007
 
"@comment POSIX.2"
2008
 
"@item REG_EXTENDED"
2009
 
"Treat the pattern as an extended regular expression, rather than as a"
2010
 
"basic regular expression."
2011
 
""
2012
 
"@comment regex.h"
2013
 
"@comment POSIX.2"
2014
 
"@item REG_ICASE"
2015
 
"Ignore case when matching letters."
2016
 
""
2017
 
"@comment regex.h"
2018
 
"@comment POSIX.2"
2019
 
"@item REG_NOSUB"
2020
 
"Don't bother storing the contents of the @var{matches-ptr} array."
2021
 
""
2022
 
"@comment regex.h"
2023
 
"@comment POSIX.2"
2024
 
"@item REG_NEWLINE"
2025
 
"Treat a newline in @var{string} as dividing @var{string} into multiple"
2026
 
"lines, so that @samp{$} can match before the newline and @samp{^} can"
2027
 
"match after.  Also, don't permit @samp{.} to match a newline, and don't"
2028
 
"permit @samp{[^@dots{}]} to match a newline."
2029
 
""
2030
 
"Otherwise, newline acts like any other ordinary character."
2031
 
"@end table"
2032
 
""
2033
 
"@node Matching POSIX Regexps, Regexp Subexpressions, Flags for POSIX Regexps, Posix Entry Points"
2034
 
"@section Matching a Compiled POSIX Regular Expression"
2035
 
""
2036
 
"Once you have compiled a regular expression, as described in @ref{POSIX"
2037
 
"Regexp Compilation}, you can match it against strings using"
2038
 
"@code{regexec}.  A match anywhere inside the string counts as success,"
2039
 
"unless the regular expression contains anchor characters (@samp{^} or"
2040
 
"@samp{$})."
2041
 
""
2042
 
"@comment regex.h"
2043
 
"@comment POSIX.2"
2044
 
"@deftypefun int regexec (regex_t *@var{compiled}, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
2045
 
"@deftypefunx int regnexec (regex_t *@var{compiled}, int len, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
2046
 
"This function tries to match the compiled regular expression"
2047
 
"@code{*@var{compiled}} against @var{string}."
2048
 
""
2049
 
"@code{regexec} returns @code{0} if the regular expression matches;"
2050
 
"otherwise, it returns a nonzero value.  See the table below for"
2051
 
"what nonzero values mean.  You can use @code{regerror} to produce an"
2052
 
"error message string describing the reason for a nonzero value; "
2053
 
"see @ref{Regexp Cleanup}."
2054
 
""
2055
 
"The parameter @var{string} points to the text to search.  When using"
2056
 
"@code{regexec}, @var{string} must be 0-terminated.  When using"
2057
 
"@code{regnexec}, @var{string} must be @var{len} characters long."
2058
 
""
2059
 
"@code{regnexec} is not a standard function;  strictly POSIX programs"
2060
 
"should avoid using it."
2061
 
""
2062
 
"The argument @var{eflags} is a word of bit flags that enable various"
2063
 
"options."
2064
 
""
2065
 
"If you want to get information about what part of @var{string} actually"
2066
 
"matched the regular expression or its subexpressions, use the arguments"
2067
 
"@var{matchptr} and @var{nmatch}.  Otherwise, pass @code{0} for "
2068
 
"@var{nmatch}, and @code{NULL} for @var{matchptr}.  @xref{Regexp"
2069
 
"Subexpressions}."
2070
 
"@end deftypefun"
2071
 
""
2072
 
"You must match the regular expression with the same set of current"
2073
 
"locales that were in effect when you compiled the regular expression."
2074
 
""
2075
 
"The function @code{regexec} accepts the following flags in the"
2076
 
"@var{eflags} argument:"
2077
 
""
2078
 
"@table @code "
2079
 
"@comment regex.h"
2080
 
"@comment POSIX.2"
2081
 
"@item REG_NOTBOL"
2082
 
"Do not regard the beginning of the specified string as the beginning of"
2083
 
"a line; more generally, don't make any assumptions about what text might"
2084
 
"precede it."
2085
 
""
2086
 
"@comment regex.h"
2087
 
"@comment POSIX.2"
2088
 
"@item REG_NOTEOL"
2089
 
"Do not regard the end of the specified string as the end of a line; more"
2090
 
"generally, don't make any assumptions about what text might follow it."
2091
 
"@end table"
2092
 
""
2093
 
"Here are the possible nonzero values that @code{regexec} can return:"
2094
 
""
2095
 
"@table @code"
2096
 
"@comment regex.h"
2097
 
"@comment POSIX.2"
2098
 
"@item REG_NOMATCH"
2099
 
"The pattern didn't match the string.  This isn't really an error."
2100
 
""
2101
 
"@comment regex.h"
2102
 
"@comment POSIX.2"
2103
 
"@item REG_ESPACE"
2104
 
"@code{regexec} ran out of memory."
2105
 
"@end table"
2106
 
""
2107
 
"@node Regexp Subexpressions, Subexpression Complications, Matching POSIX Regexps, Posix Entry Points"
2108
 
"@section Match Results with Subexpressions"
2109
 
""
2110
 
"When @code{regexec} matches parenthetical subexpressions of"
2111
 
"@var{pattern}, it records which parts of @var{string} they match.  It"
2112
 
"returns that information by storing the offsets into an array whose"
2113
 
"elements are structures of type @code{regmatch_t}.  The first element of"
2114
 
"the array (index @code{0}) records the part of the string that matched"
2115
 
"the entire regular expression.  Each other element of the array records"
2116
 
"the beginning and end of the part that matched a single parenthetical"
2117
 
"subexpression."
2118
 
""
2119
 
"@comment regex.h"
2120
 
"@comment POSIX.2"
2121
 
"@deftp {Data Type} regmatch_t"
2122
 
"This is the data type of the @var{matcharray} array that you pass to"
2123
 
"@code{regexec}.  It containes two structure fields, as follows:"
2124
 
""
2125
 
"@table @code"
2126
 
"@item rm_so"
2127
 
"The offset in @var{string} of the beginning of a substring.  Add this"
2128
 
"value to @var{string} to get the address of that part."
2129
 
""
2130
 
"@item rm_eo"
2131
 
"The offset in @var{string} of the end of the substring."
2132
 
"@end table"
2133
 
"@end deftp"
2134
 
""
2135
 
"@comment regex.h"
2136
 
"@comment POSIX.2"
2137
 
"@deftp {Data Type} regoff_t"
2138
 
"@code{regoff_t} is an alias for another signed integer type."
2139
 
"The fields of @code{regmatch_t} have type @code{regoff_t}."
2140
 
"@end deftp"
2141
 
""
2142
 
"The @code{regmatch_t} elements correspond to subexpressions"
2143
 
"positionally; the first element (index @code{1}) records where the first"
2144
 
"subexpression matched, the second element records the second"
2145
 
"subexpression, and so on.  The order of the subexpressions is the order"
2146
 
"in which they begin."
2147
 
""
2148
 
"When you call @code{regexec}, you specify how long the @var{matchptr}"
2149
 
"array is, with the @var{nmatch} argument.  This tells @code{regexec} how"
2150
 
"many elements to store.  If the actual regular expression has more than"
2151
 
"@var{nmatch} subexpressions, then you won't get offset information about"
2152
 
"the rest of them.  But this doesn't alter whether the pattern matches a"
2153
 
"particular string or not."
2154
 
""
2155
 
"If you don't want @code{regexec} to return any information about where"
2156
 
"the subexpressions matched, you can either supply @code{0} for"
2157
 
"@var{nmatch}, or use the flag @code{REG_NOSUB} when you compile the"
2158
 
"pattern with @code{regcomp}."
2159
 
""
2160
 
"@node Subexpression Complications, Regexp Cleanup, Regexp Subexpressions, Posix Entry Points"
2161
 
"@section Complications in Subexpression Matching"
2162
 
""
2163
 
"Sometimes a subexpression matches a substring of no characters.  This"
2164
 
"happens when @samp{f\\(o*\\)} matches the string @samp{fum}.  (It really"
2165
 
"matches just the @samp{f}.)  In this case, both of the offsets identify"
2166
 
"the point in the string where the null substring was found.  In this"
2167
 
"example, the offsets are both @code{1}."
2168
 
""
2169
 
"Sometimes the entire regular expression can match without using some of"
2170
 
"its subexpressions at all---for example, when @samp{ba\\(na\\)*} matches the"
2171
 
"string @samp{ba}, the parenthetical subexpression is not used.  When"
2172
 
"this happens, @code{regexec} stores @code{-1} in both fields of the"
2173
 
"element for that subexpression."
2174
 
""
2175
 
"Sometimes matching the entire regular expression can match a particular"
2176
 
"subexpression more than once---for example, when @samp{ba\\(na\\)*}"
2177
 
"matches the string @samp{bananana}, the parenthetical subexpression"
2178
 
"matches three times.  When this happens, @code{regexec} usually stores"
2179
 
"the offsets of the last part of the string that matched the"
2180
 
"subexpression.  In the case of @samp{bananana}, these offsets are"
2181
 
"@code{6} and @code{8}."
2182
 
""
2183
 
"But the last match is not always the one that is chosen.  It's more"
2184
 
"accurate to say that the last @emph{opportunity} to match is the one"
2185
 
"that takes precedence.  What this means is that when one subexpression"
2186
 
"appears within another, then the results reported for the inner"
2187
 
"subexpression reflect whatever happened on the last match of the outer"
2188
 
"subexpression.  For an example, consider @samp{\\(ba\\(na\\)*s \\)*} matching"
2189
 
"the string @samp{bananas bas }.  The last time the inner expression"
2190
 
"actually matches is near the end of the first word.  But it is "
2191
 
"@emph{considered} again in the second word, and fails to match there."
2192
 
"@code{regexec} reports nonuse of the ``na'' subexpression."
2193
 
""
2194
 
"Another place where this rule applies is when the regular expression"
2195
 
"@w{@samp{\\(ba\\(na\\)*s \\|nefer\\(ti\\)* \\)*}} matches @samp{bananas nefertiti}."
2196
 
"The ``na'' subexpression does match in the first word, but it doesn't"
2197
 
"match in the second word because the other alternative is used there."
2198
 
"Once again, the second repetition of the outer subexpression overrides"
2199
 
"the first, and within that second repetition, the ``na'' subexpression"
2200
 
"is not used.  So @code{regexec} reports nonuse of the ``na''"
2201
 
"subexpression."
2202
 
""
2203
 
"@node Regexp Cleanup,  , Subexpression Complications, Posix Entry Points"
2204
 
"@section POSIX Regexp Matching Cleanup"
2205
 
""
2206
 
"When you are finished using a compiled regular expression, you can"
2207
 
"free the storage it uses by calling @code{regfree}."
2208
 
""
2209
 
"@comment regex.h"
2210
 
"@comment POSIX.2"
2211
 
"@deftypefun void regfree (regex_t *@var{compiled})"
2212
 
"Calling @code{regfree} frees all the storage that @code{*@var{compiled}}"
2213
 
"points to.  This includes various internal fields of the @code{regex_t}"
2214
 
"structure that aren't documented in this manual."
2215
 
""
2216
 
"@code{regfree} does not free the object @code{*@var{compiled}} itself."
2217
 
"@end deftypefun"
2218
 
""
2219
 
"You should always free the space in a @code{regex_t} structure with"
2220
 
"@code{regfree} before using the structure to compile another regular"
2221
 
"expression."
2222
 
""
2223
 
"When @code{regcomp} or @code{regexec} reports an error, you can use"
2224
 
"the function @code{regerror} to turn it into an error message string."
2225
 
""
2226
 
"@comment regex.h"
2227
 
"@comment POSIX.2"
2228
 
"@deftypefun size_t regerror (int @var{errcode}, regex_t *@var{compiled}, char *@var{buffer}, size_t @var{length})"
2229
 
"This function produces an error message string for the error code"
2230
 
"@var{errcode}, and stores the string in @var{length} bytes of memory"
2231
 
"starting at @var{buffer}.  For the @var{compiled} argument, supply the"
2232
 
"same compiled regular expression structure that @code{regcomp} or"
2233
 
"@code{regexec} was working with when it got the error.  Alternatively,"
2234
 
"you can supply @code{NULL} for @var{compiled}; you will still get a"
2235
 
"meaningful error message, but it might not be as detailed."
2236
 
""
2237
 
"If the error message can't fit in @var{length} bytes (including a"
2238
 
"terminating null character), then @code{regerror} truncates it."
2239
 
"The string that @code{regerror} stores is always null-terminated"
2240
 
"even if it has been truncated."
2241
 
""
2242
 
"The return value of @code{regerror} is the minimum length needed to"
2243
 
"store the entire error message.  If this is less than @var{length}, then"
2244
 
"the error message was not truncated, and you can use it.  Otherwise, you"
2245
 
"should call @code{regerror} again with a larger buffer."
2246
 
""
2247
 
"Here is a function which uses @code{regerror}, but always dynamically"
2248
 
"allocates a buffer for the error message:"
2249
 
""
2250
 
"@smallexample"
2251
 
"char *get_regerror (int errcode, regex_t *compiled)"
2252
 
"@{"
2253
 
"  size_t length = regerror (errcode, compiled, NULL, 0);"
2254
 
"  char *buffer = xmalloc (length);"
2255
 
"  (void) regerror (errcode, compiled, buffer, length);"
2256
 
"  return buffer;"
2257
 
"@}"
2258
 
"@end smallexample"
2259
 
"@end deftypefun"
2260
 
""
2261
 
"@node  Beyond POSIX, Rx Theory, Posix Entry Points, Top"
2262
 
"@chapter Beyond POSIX"
2263
 
""
2264
 
"This section is not finished documentation, but rather a collection of"
2265
 
"pointers towards some of the interesting, non-standard features of Rx."
2266
 
""
2267
 
"@section New Regexp Operators"
2268
 
""
2269
 
"Rx supports some unusual regexp syntax."
2270
 
""
2271
 
"@code{[[:cut N:]]} sets @code{pmatch[0].final_tag} to N and causes the"
2272
 
"matching to stop instantly.  If N is 0, the overall match fails,"
2273
 
"otherwise it succeeds."
2274
 
""
2275
 
"@code{[[:(:]] ... [[:):]]} is just like @code{\\( ... \\)} except that in"
2276
 
"the first case, no pmatch entries are changed, and the subexpression is"
2277
 
"not counted in the numbering of parenthesized subexpressions."
2278
 
""
2279
 
"@code{[[:(:]] ... [[:):]]} can be used when you do not need to know"
2280
 
"where a subexpression matched but are only using parentheses to effect"
2281
 
"the parsing of the regexp.  "
2282
 
""
2283
 
"There are two reasons to use @code{[[:(:]] ... [[:):]]}:"
2284
 
""
2285
 
"1. regexec will run faster."
2286
 
""
2287
 
"2. Currently, only 8 backreferencable subexpressions are supported:"
2288
 
"@code{\\1 .. \\9}.  Using @code{[[:(:]] ... [[:):]]} is a way to conserve"
2289
 
"backreferencable subexpression names in an expression with many"
2290
 
"parentheses."
2291
 
""
2292
 
"@section New POSIX Functions"
2293
 
""
2294
 
"@code{regncomp} and @code{regnexec} are non-standard generalizations of"
2295
 
"@code{regcomp} and @code{regexec}."
2296
 
""
2297
 
"@section Tuning POSIX performance"
2298
 
""
2299
 
"Two mysterious parmaters can be used to trade-off performance and"
2300
 
"memory use."
2301
 
""
2302
 
"At compile-time they are @code{RX_DEFAULT_DFA_CACHE_SIZE} and"
2303
 
"@code{RX_DEFAULT_NFA_DELAY}.  "
2304
 
""
2305
 
"If you want to mess with these (I generally don't advise it), I suggest"
2306
 
"experimenting for your particular application/memory situation; frob"
2307
 
"these by powers of two and try out the results on what you expect will"
2308
 
"be typical regexp workloads."
2309
 
""
2310
 
"You can also set those parameters at run-time (before calling any regexp"
2311
 
"functions) by tweaking the corresponding variables:"
2312
 
""
2313
 
"@code{rx_default_cache->bytes_allowed}"
2314
 
""
2315
 
"and"
2316
 
""
2317
 
"@code{rx_basic_unfaniverse_delay}"
2318
 
""
2319
 
""
2320
 
""
2321
 
"@section POSIX stream-style interface"
2322
 
""
2323
 
"@code{rx_make_solutions}, @code{rx_next_solution}, and"
2324
 
"@code{rx_free_solutions} are a lower level alternative to the posix"
2325
 
"functions.  Using those functions, you can compare a compiled regexp to"
2326
 
"a string that is not contiguous in memory or even a string that is not"
2327
 
"entirely in memory at any one time."
2328
 
""
2329
 
"The code in rxposix.c points out how those functions are used."
2330
 
""
2331
 
""
2332
 
"@section DFAs Directly"
2333
 
""
2334
 
"If you are only interested in pure regular expressions (no pmatch data,"
2335
 
"no backreferences, and no counted subexpressions), you can parse a"
2336
 
"regexp using @code{rx_parse}, convert it to an nfa using @code{rx_unfa},"
2337
 
"and run the dfa using @code{rx_init_system}, @code{rx_advance_to_final},"
2338
 
"and @code{rx_terminate_system}.  The dfa Scheme primitives in"
2339
 
"@file{rgx.c} may provide some guide."
2340
 
""
2341
 
"@node    Rx Theory,  , Beyond POSIX, Top"
2342
 
"@chapter Rx Theory"
2343
 
"@cindex Rx Theory"
2344
 
""
2345
 
"There are two match algorithms.  One is for truly regular regexps (those"
2346
 
"that can be reduced to a dfa).  The other is for non-regular regexps."
2347
 
""
2348
 
"The dfa algorithm implements the idea suggested in @cite{Compilers} by"
2349
 
"Aho, Sethi and Ullman:"
2350
 
""
2351
 
"@quotation"
2352
 
"[One] approach [to pattern matching regular expressions] is to use a"
2353
 
"DFA, but avoid constructing all of the transition table by using a"
2354
 
"technique called \"lazy transition evaluation\".  Here, transitions are"
2355
 
"computed at run time [when] actually needed. [T]ransitions are"
2356
 
"stored in a cache. [....] If the cache becomes full, we can erase some"
2357
 
"previously computed transition to make room for the new transition."
2358
 
"@end quotation"
2359
 
""
2360
 
"The implementation in Rx is generalized from that, but the above"
2361
 
"description covers what is used for Posix patterns. "
2362
 
""
2363
 
"The non-dfa algorithm implements a \"recursive decomposition\" technique"
2364
 
"described in email by Henry Spencer.  For a given pattern, this"
2365
 
"algorithm first checks to see if a simpler, superset language,"
2366
 
"DFA-pattern matches.  If it does, then this algorithm does the"
2367
 
"detail-work to see if the non-DFA pattern matches."
2368
 
"\\input texinfo   @c -*-texinfo-*-"
2369
 
"@c %**start of header"
2370
 
"@setfilename rx.info"
2371
 
"@settitle Rx"
2372
 
"@setchapternewpage odd"
2373
 
"@c %**end of header"
2374
 
""
2375
 
"@c  This title page illustrates only one of the"
2376
 
"@c  two methods of forming a title page."
2377
 
""
2378
 
"@titlepage"
2379
 
"@title Rx"
2380
 
"@author Tom Lord"
2381
 
""
2382
 
"@center except the chapter \"Posix Entry Points\" "
2383
 
"@center from @emph{The GNU C Library} reference manual"
2384
 
"@center by Sandra Loosemore"
2385
 
"@center with"
2386
 
"@center Richard M. Stallman, Roland McGrath, and Andrew Oram"
2387
 
""
2388
 
"@c  The following two commands"
2389
 
"@c  start the copyright page."
2390
 
"@page"
2391
 
"@vskip 0pt plus 1filll"
2392
 
"Copyright @copyright{} 1995 Cygnus Support"
2393
 
""
2394
 
"except the chapter \"Posix Entry Points\" which is:"
2395
 
""
2396
 
"Copyright @copyright{} 1995 Free Software Foundation, Inc."
2397
 
""
2398
 
"Permission is granted to make and distribute verbatim copies of"
2399
 
"this manual provided the copyright notice and this permission notice"
2400
 
"are preserved on all copies."
2401
 
""
2402
 
"Permission is granted to copy and distribute modified versions of this"
2403
 
"manual under the conditions for verbatim copying, provided that the entire"
2404
 
"resulting derived work is distributed under the terms of a permission"
2405
 
"notice identical to this one."
2406
 
""
2407
 
"Permission is granted to copy and distribute translations of this manual"
2408
 
"into another language, under the above conditions for modified versions,"
2409
 
"except that this permission notice may be stated in a translation approved"
2410
 
"by the author."
2411
 
""
2412
 
"@end titlepage"
2413
 
""
2414
 
"@node Top, Copying, (dir), (dir)"
2415
 
"@top Rx"
2416
 
""
2417
 
"This document describes Rx."
2418
 
""
2419
 
"@menu"
2420
 
"* Copying::                            Sharing is good."
2421
 
"* Overview::                           Fnord"
2422
 
"* Posix Basic Regular Expressions::    A popular regexp syntax."
2423
 
"* Posix Entry Points::                 The POSIX way to regexp."
2424
 
"* Beyond POSIX::                       Hints about cool features."
2425
 
"* Rx Theory::                          Hints about how it works."
2426
 
"@end menu"
2427
 
""
2428
 
"@node Copying, Overview, Top, Top"
2429
 
"@chapter Copying"
2430
 
""
2431
 
"@center Copyright (C) 1996"
2432
 
"@center Tom Lord"
2433
 
"@center Berkeley, CA USA"
2434
 
""
2435
 
"@center except portions of \"POSIX Regex Functions\" which are"
2436
 
"@center Copyright (C) 1995"
2437
 
"@center Free Software Foundation, Inc."
2438
 
""
2439
 
"@noindent"
2440
 
"Permission to use, copy, modify, distribute, and sell this software and"
2441
 
"its documentation for any purpose is hereby granted without fee,"
2442
 
"provided that the above copyright notice appear in all copies and that"
2443
 
"both that copyright notice and this permission notice appear in"
2444
 
"supporting documentation."
2445
 
""
2446
 
"@center NO WARRANTY"
2447
 
""
2448
 
"@noindent"
2449
 
"BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR"
2450
 
"THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN"
2451
 
"OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES"
2452
 
"PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER"
2453
 
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED"
2454
 
"WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE"
2455
 
"ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH"
2456
 
"YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL"
2457
 
"NECESSARY SERVICING, REPAIR OR CORRECTION."
2458
 
""
2459
 
"@noindent"
2460
 
"IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING"
2461
 
"WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR"
2462
 
"REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR"
2463
 
"DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL"
2464
 
"DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM"
2465
 
"(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED"
2466
 
"INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF"
2467
 
"THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR"
2468
 
"OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES."
2469
 
""
2470
 
"@node  Overview, Posix Basic Regular Expressions, Copying, Top"
2471
 
"@chapter Overview"
2472
 
""
2473
 
"Nothing to say here, yet."
2474
 
""
2475
 
"@node  Posix Basic Regular Expressions, Posix Entry Points, Overview, Top"
2476
 
"@chapter Posix Basic Regular Expressions"
2477
 
""
2478
 
"The Posix Basic Regular Expression language is a notation for describing"
2479
 
"textual patterns.  Regexps are typically used by comparing them to a"
2480
 
"string to see if that string matches the pattern, or by searching within"
2481
 
"a string for a substring that matches."
2482
 
""
2483
 
"This chapter introduces the Posix regexp notation.  This is not a formal"
2484
 
"or precise definition of Posix regexps -- it is an intuitive and"
2485
 
"hopefully expository description of them."
2486
 
""
2487
 
"@menu"
2488
 
"* An Introduction to Regexps::  "
2489
 
"* Literal Regexps::             "
2490
 
"* Character Sets::              "
2491
 
"* Subexpressions::              "
2492
 
"* Repeated Subexpressions::     "
2493
 
"* Optional Subexpressions::     "
2494
 
"* Counted Subexpressions::      "
2495
 
"* Alternative Subexpressions::  "
2496
 
"* Backreferences::              "
2497
 
"* A Summary of Regexp Syntax::  "
2498
 
"* Ambiguous Patterns::          "
2499
 
"@end menu"
2500
 
""
2501
 
""
2502
 
"@node  An Introduction to Regexps, Literal Regexps, Posix Basic Regular Expressions, Posix Basic Regular Expressions"
2503
 
"@section An Introduction to Regexps"
2504
 
""
2505
 
"In the simplest cases, a regexp is just a literal string that must"
2506
 
"match exactly.   For example, the pattern:"
2507
 
""
2508
 
"@example"
2509
 
"regexp"
2510
 
"@end example"
2511
 
""
2512
 
"matches the string \"regexp\" and no others."
2513
 
""
2514
 
"Some characters have a special meaning when they occur in a regexp."
2515
 
"They aren't matched literally as in the previous example, but instead"
2516
 
"denote a more general pattern.   For example, the character @code{*}"
2517
 
"is used to indicate that the preceeding element of a regexp may be"
2518
 
"repeated 0, 1, or more times.  In the pattern:"
2519
 
""
2520
 
"@example"
2521
 
"smooo*th"
2522
 
"@end example"
2523
 
""
2524
 
"the @code{*} indicates that the preceeding @code{o} can be repeated 0 or"
2525
 
"more times.   So the pattern matches:"
2526
 
""
2527
 
"@example"
2528
 
"smooth"
2529
 
"smoooth"
2530
 
"smooooth"
2531
 
"smoooooth"
2532
 
"..."
2533
 
"@end example"
2534
 
""
2535
 
"Suppose you want to write a pattern that literally matches a special"
2536
 
"character like @code{*} -- in other words, you don't want to @code{*} to"
2537
 
"indicate a permissible repetition, but to match @code{*} literally.  This"
2538
 
"is accomplished by quoting the special character with a backslash.  "
2539
 
"The pattern:"
2540
 
""
2541
 
"@example"
2542
 
"smoo\\*th"
2543
 
"@end example"
2544
 
""
2545
 
"matches the string:"
2546
 
""
2547
 
"@example"
2548
 
"smoo*th"
2549
 
"@end example"
2550
 
""
2551
 
""
2552
 
"and no other strings."
2553
 
""
2554
 
"In five cases, the pattern is reversed -- a backslash makes the"
2555
 
"character special instead of making a special character normal.  The"
2556
 
"characters @code{+}, @code{?}, @code{|}, @code{(}, and @code{)} are"
2557
 
"normal but the sequences @code{\\+}, @code{\\?}, @code{\\|}, @code{\\(}, and"
2558
 
"@code{\\)} are special (their meaning is described later)."
2559
 
""
2560
 
"The remaining sections of this chapter introduce and explain the various"
2561
 
"special characters that can occur in regexps."
2562
 
""
2563
 
"@node  Literal Regexps, Character Sets, An Introduction to Regexps, Posix Basic Regular Expressions"
2564
 
"@section Literal Regexps"
2565
 
""
2566
 
"A literal regexp is a string which contains no special characters."
2567
 
"A literal regexp matches an identical string, but no other characters."
2568
 
"For example:"
2569
 
""
2570
 
"@example"
2571
 
"literally"
2572
 
"@end example"
2573
 
""
2574
 
"matches"
2575
 
""
2576
 
"@example"
2577
 
"literally"
2578
 
"@end example"
2579
 
""
2580
 
"and nothing else."
2581
 
""
2582
 
"Generally, whitespace characters, numbers, and letters are not special."
2583
 
"Some punctuation characters are special and some are not (the syntax"
2584
 
"summary at the end of this chapter makes a convenient reference for"
2585
 
"which characters are special and which aren't)."
2586
 
""
2587
 
""
2588
 
"@node  Character Sets, Subexpressions, Literal Regexps, Posix Basic Regular Expressions"
2589
 
"@section Character Sets"
2590
 
""
2591
 
"This section introduces the special characters @code{.} and @code{[}."
2592
 
""
2593
 
"@code{.} matches any character except the NULL character.  For example:"
2594
 
""
2595
 
"@example"
2596
 
"p.ck"
2597
 
"@end example"
2598
 
""
2599
 
"matches"
2600
 
""
2601
 
"@example"
2602
 
"pick"
2603
 
"pack"
2604
 
"puck"
2605
 
"pbck"
2606
 
"pcck"
2607
 
"p.ck"
2608
 
""
2609
 
"..."
2610
 
"@end example"
2611
 
""
2612
 
""
2613
 
"@code{[} begins a @dfn{character set}.  A character set is similar to"
2614
 
"@code{.} in that it matches not a single, literal character, but any"
2615
 
"of a set of characters.   @code{[} is different from @code{.} in that"
2616
 
"with @code{[}, you define the set of characters explicitly."
2617
 
""
2618
 
"There are three basic forms a character set can take."
2619
 
""
2620
 
"In the first form, the character set is spelled out:"
2621
 
""
2622
 
"@example"
2623
 
"[<cset-spec>]  -- every character in <cset-spec> is in the set."
2624
 
"@end example"
2625
 
""
2626
 
"In the second form, the character set indicated is the negation of"
2627
 
"a character set is explicitly spelled out:"
2628
 
""
2629
 
"@example"
2630
 
"[^<cset-spec>] -- every character *not* in <cset-spec> is in the set."
2631
 
"@end example"
2632
 
""
2633
 
"A @code{<cset-spec>} is more or less an explicit enumeration of a set"
2634
 
"of characters.  It can be written as a string of individual characters:"
2635
 
""
2636
 
"@example"
2637
 
"[aeiou]"
2638
 
"@end example"
2639
 
""
2640
 
"or as a range of characters:"
2641
 
""
2642
 
"@example"
2643
 
"[0-9]"
2644
 
"@end example"
2645
 
""
2646
 
"These two forms can be mixed:"
2647
 
""
2648
 
"@example"
2649
 
"[A-za-z0-9_$]"
2650
 
"@end example"
2651
 
""
2652
 
"Note that special regexp characters (such as @code{*}) are @emph{not}"
2653
 
"special within a character set.  @code{-}, as illustrated above,"
2654
 
"@emph{is} special, except, as illustrated below, when it is the first"
2655
 
"character mentioned."
2656
 
""
2657
 
"This is a four-character set:"
2658
 
""
2659
 
"@example"
2660
 
"[-+*/]"
2661
 
"@end example"
2662
 
""
2663
 
"The third form of a character set makes use of a pre-defined \"character"
2664
 
"class\":"
2665
 
""
2666
 
"@example"
2667
 
"[[:class-name:]] -- every character described by class-name is in the set."
2668
 
"@end example"
2669
 
""
2670
 
"The supported character classes are:"
2671
 
""
2672
 
"@example"
2673
 
"alnum  - the set of alpha-numeric characters"
2674
 
"alpha  - the set of alphabetic characters"
2675
 
"blank  - tab and space"
2676
 
"cntrl  - the control characters"
2677
 
"digit  - decimal digits"
2678
 
"graph  - all printable characters except space"
2679
 
"lower  - lower case letters"
2680
 
"print  - the \"printable\" characters"
2681
 
"punct  - punctuation"
2682
 
"space  - whitespace characters"
2683
 
"upper  - upper case letters"
2684
 
"xdigit - hexidecimal digits"
2685
 
"@end example"
2686
 
""
2687
 
"Finally, character class sets can also be inverted:"
2688
 
""
2689
 
"@example"
2690
 
"[^[:space:]] - all non-whitespace characters"
2691
 
"@end example"
2692
 
""
2693
 
"Character sets can be used in a regular expression anywhere a literal"
2694
 
"character can."
2695
 
""
2696
 
"@node  Subexpressions, Repeated Subexpressions, Character Sets, Posix Basic Regular Expressions"
2697
 
"@section Subexpressions"
2698
 
""
2699
 
"A subexpression is a regular expression enclosed in @code{\\(} and"
2700
 
"@code{\\)}.  A subexpression can be used anywhere a single character or"
2701
 
"character set can be used."
2702
 
""
2703
 
"Subexpressions are useful for grouping regexp constructs.  For example,"
2704
 
"the repeat operator, @code{*}, usually applies to just the preceeding"
2705
 
"character.   Recall that:"
2706
 
""
2707
 
"@example"
2708
 
"smooo*th"
2709
 
"@end example"
2710
 
""
2711
 
"matches"
2712
 
""
2713
 
"@example"
2714
 
"smooth"
2715
 
"smoooth"
2716
 
"..."
2717
 
"@end example"
2718
 
""
2719
 
""
2720
 
"Using a subexpression, we can apply @code{*} to a longer string:"
2721
 
""
2722
 
"@example"
2723
 
"banan\\(an\\)*a"
2724
 
"@end example"
2725
 
""
2726
 
"matches"
2727
 
""
2728
 
"@example"
2729
 
"banana"
2730
 
"bananana"
2731
 
"banananana"
2732
 
"..."
2733
 
"@end example"
2734
 
""
2735
 
"Subexpressions also have a special meaning with regard to backreferences"
2736
 
"and substitutions (see @xref{Backreferences})."
2737
 
""
2738
 
""
2739
 
"@node  Repeated Subexpressions, Optional Subexpressions, Subexpressions, Posix Basic Regular Expressions"
2740
 
"@section Repeated Subexpressions"
2741
 
""
2742
 
""
2743
 
"@code{*} is the repeat operator.  It applies to the preceeding"
2744
 
"character, character set, subexpression or backreference.  It indicates"
2745
 
"that the preceeding element can be matched 0 or more times:"
2746
 
""
2747
 
"@example"
2748
 
"bana\\(na\\)*"
2749
 
"@end example"
2750
 
""
2751
 
"matches"
2752
 
""
2753
 
"@example"
2754
 
"bana"
2755
 
"banana"
2756
 
"bananana"
2757
 
"banananana"
2758
 
"..."
2759
 
"@end example"
2760
 
""
2761
 
""
2762
 
""
2763
 
"@code{\\+} is similar to @code{*} except that @code{\\+} requires the"
2764
 
"preceeding element to be matched at least once.  So while:"
2765
 
""
2766
 
"@example"
2767
 
"bana\\(na\\)*"
2768
 
"@end example"
2769
 
""
2770
 
"matches"
2771
 
""
2772
 
"@example"
2773
 
"bana"
2774
 
"@end example"
2775
 
""
2776
 
"@example"
2777
 
"bana(na\\)\\+"
2778
 
"@end example"
2779
 
""
2780
 
"does not.   Both match "
2781
 
""
2782
 
"@example"
2783
 
"banana"
2784
 
"bananana"
2785
 
"banananana"
2786
 
"..."
2787
 
"@end example"
2788
 
""
2789
 
"Thus, @code{bana\\(na\\)+} is short-hand for @code{banana\\(na\\)*}."
2790
 
""
2791
 
""
2792
 
"@node  Optional Subexpressions, Counted Subexpressions, Repeated Subexpressions, Posix Basic Regular Expressions"
2793
 
"@section Optional Subexpressions"
2794
 
""
2795
 
"@code{\\?} indicates that the preceeding character, character set, or"
2796
 
"subexpression is optional.  It is permitted to match, or to be skipped:"
2797
 
""
2798
 
"@example"
2799
 
"CSNY\\?"
2800
 
"@end example"
2801
 
""
2802
 
"matches both"
2803
 
""
2804
 
"@example"
2805
 
"CSN"
2806
 
"@end example"
2807
 
""
2808
 
"and "
2809
 
""
2810
 
"@example"
2811
 
"CSNY"
2812
 
"@end example"
2813
 
""
2814
 
""
2815
 
"@node  Counted Subexpressions, Alternative Subexpressions, Optional Subexpressions, Posix Basic Regular Expressions"
2816
 
"@section Counted Subexpressions"
2817
 
""
2818
 
"An interval expression, @code{@{m,n@}} where @code{m} and @code{n} are"
2819
 
"non-negative integers with @code{n >= m}, applies to the preceeding"
2820
 
"character, character set, subexpression or backreference.  It indicates"
2821
 
"that the preceeding element must match at least @code{m} times and may"
2822
 
"match as many as @code{n} times."
2823
 
""
2824
 
"For example:"
2825
 
""
2826
 
"@example"
2827
 
"c\\([ad]\\)@{1,4@}"
2828
 
"@end example"
2829
 
""
2830
 
"matches"
2831
 
""
2832
 
"@example"
2833
 
"car"
2834
 
"cdr"
2835
 
"caar"
2836
 
"cdar"
2837
 
"..."
2838
 
"caaar"
2839
 
"cdaar"
2840
 
"..."
2841
 
"cadddr"
2842
 
"cddddr"
2843
 
"@end example"
2844
 
""
2845
 
""
2846
 
""
2847
 
"@node  Alternative Subexpressions, Backreferences, Counted Subexpressions, Posix Basic Regular Expressions"
2848
 
"@section Alternative Subexpressions"
2849
 
""
2850
 
"An alternative is written:"
2851
 
""
2852
 
"@example"
2853
 
"regexp-1\\|regexp-2\\|regexp-3\\|..."
2854
 
"@end example"
2855
 
""
2856
 
"It matches anything matched by some @code{regexp-n}.  For example:"
2857
 
""
2858
 
"@example"
2859
 
"Crosby, Stills, \\(and Nash\\|Nash, and Young\\)"
2860
 
"@end example"
2861
 
""
2862
 
"matches"
2863
 
""
2864
 
"@example"
2865
 
"Crosby, Stills, and Nash"
2866
 
"@end example"
2867
 
""
2868
 
"and"
2869
 
""
2870
 
"@example"
2871
 
"Crosby, Stills, Nash, and Young"
2872
 
"@end example"
2873
 
""
2874
 
""
2875
 
"@node  Backreferences, A Summary of Regexp Syntax, Alternative Subexpressions, Posix Basic Regular Expressions"
2876
 
"@section Backreferences, Extractions and Substitutions"
2877
 
""
2878
 
"A backreference is written @code{\\n} where @code{n} is some single digit"
2879
 
"other than 0.  To be a valid backreference, there must be at least"
2880
 
"@code{n} parenthesized subexpressions in the pattern prior to the"
2881
 
"backreference."
2882
 
""
2883
 
"A backreference matches a literal copy of whatever was matched by the"
2884
 
"corresponding subexpression.  For example,"
2885
 
""
2886
 
"@example"
2887
 
"\\(.*\\)-\\1"
2888
 
"@end example"
2889
 
""
2890
 
"matches:"
2891
 
""
2892
 
"@example"
2893
 
"go-go"
2894
 
"ha-ha"
2895
 
"wakka-wakka"
2896
 
"..."
2897
 
"@end example"
2898
 
""
2899
 
"In some applications, subexpressions are used to extract substrings."
2900
 
"For example, Emacs has the functions @code{match-beginnning} and"
2901
 
"@code{match-end} which report the positions of strings matched by"
2902
 
"subexpressions.  These functions use the same numbering scheme for"
2903
 
"subexpressions as backreferences, with the additional rule that"
2904
 
"subexpression 0 is defined to be the whole regexp."
2905
 
""
2906
 
"In some applications, subexpressions are used in string substitution."
2907
 
"This again uses the backreference numbering scheme. For example, this sed"
2908
 
"command:"
2909
 
""
2910
 
"@example"
2911
 
"s/From:.*<\\(.*\\)>/To: \\1/"
2912
 
"@end example"
2913
 
""
2914
 
"first matches the line:"
2915
 
""
2916
 
"@example"
2917
 
"From: Joe Schmoe <schmoe@@uspringfield.edu>"
2918
 
"@end example"
2919
 
""
2920
 
"when it does, subexpression 1 matches \"schmoe@@uspringfield.edu\"."
2921
 
"The command replaces the matched line with \"To: \\1\" after doing"
2922
 
"subexpression substitution on it to get:"
2923
 
""
2924
 
"@example"
2925
 
"To: schmoe@@uspringfield.edu"
2926
 
"@end example"
2927
 
""
2928
 
""
2929
 
"@node  A Summary of Regexp Syntax, Ambiguous Patterns, Backreferences, Posix Basic Regular Expressions"
2930
 
"@section A Summary of Regexp Syntax"
2931
 
""
2932
 
"In summary, regexps can be:"
2933
 
""
2934
 
"@code{abcd} -- matching a string literally"
2935
 
""
2936
 
"@code{.} -- matching everything except NULL"
2937
 
""
2938
 
"@code{[a-z_?]}, @code{^[a-z_?]}, @code{[[:alpha:]]} and"
2939
 
"@code{[^[:alpha:]]} -- matching character sets"
2940
 
""
2941
 
"@code{\\(subexp\\)} -- grouping an expression into a subexpression."
2942
 
""
2943
 
"@code{\\n} -- match a copy of whatever was matched by the nth subexpression."
2944
 
""
2945
 
"The following special characters and sequences can be applied to a"
2946
 
"character, character set, subexpression, or backreference:"
2947
 
""
2948
 
"@code{*} -- repeat the preceeding element 0 or more times."
2949
 
""
2950
 
"@code{\\+} -- repeat the preceeding element 1 or more times."
2951
 
""
2952
 
"@code{\\?} -- match the preceeding element 0 or 1 time."
2953
 
""
2954
 
"@code{@{m,n@}} -- match the preceeding element at least @code{m}, and as"
2955
 
"many as @code{n} times."
2956
 
""
2957
 
"@code{regexp-1\\|regexp-2\\|..} -- match any regexp-n."
2958
 
""
2959
 
"A special character, like @code{.} or @code{*} can be made into a literal"
2960
 
"character by prefixing it with @code{\\}."
2961
 
""
2962
 
"A special sequence, like @code{\\+} or @code{\\?} can be made into a"
2963
 
"literal character by dropping the @code{\\}."
2964
 
""
2965
 
""
2966
 
"@node  Ambiguous Patterns,  , A Summary of Regexp Syntax, Posix Basic Regular Expressions"
2967
 
"@section Ambiguous Patterns"
2968
 
""
2969
 
"Sometimes a regular expression appears to be ambiguous.  For example, suppose"
2970
 
"we compare the pattern:"
2971
 
""
2972
 
"@example"
2973
 
"begin\\|beginning"
2974
 
"@end example"
2975
 
""
2976
 
"to the string"
2977
 
""
2978
 
"@example"
2979
 
"beginning"
2980
 
"@end example"
2981
 
""
2982
 
"either just the first 5 characters will match, or the whole string will match."
2983
 
""
2984
 
"In every case like this, the longer match is preferred.  The whole"
2985
 
"string will match."
2986
 
""
2987
 
"Sometimes there is ambiguity not about how many characters to match, but"
2988
 
"where the subexpressions occur within the match.  This can effect"
2989
 
"extraction functions like Emacs' @code{match-beginning} or rewrite"
2990
 
"functions like sed's @code{s} command.  For example, consider matching"
2991
 
"the pattern:"
2992
 
""
2993
 
"@example"
2994
 
"b\\(\\[^q]*\\)\\(ing\\)?"
2995
 
"@end example"
2996
 
""
2997
 
"against the string"
2998
 
""
2999
 
"@example"
3000
 
"beginning"
3001
 
"@end example"
3002
 
""
3003
 
"One possibility is that the first subexpression matches \"eginning\" and"
3004
 
"the second is skipped.  Another possibility is that the first"
3005
 
"subexpression matches \"eginn\" and the second matches \"ing\"."
3006
 
""
3007
 
"The rule is that consistant with matching as many characters as "
3008
 
"possible, the length of lower numbered subexpressions is maximized"
3009
 
"in preference to maximizing the length of later subexpressions."
3010
 
""
3011
 
"In the case of the above example, the two possible matches are equal in"
3012
 
"overall length.  Therefore, it comes down to maximizing the"
3013
 
"lower-numbered subexpression, \\1.  The correct answer is that \\1 matches"
3014
 
"\"eginning\" and \\2 is skipped."
3015
 
""
3016
 
"@node Posix Entry Points, Beyond POSIX, Posix Basic Regular Expressions, Top"
3017
 
"@chapter Posix Entry Points"
3018
 
""
3019
 
"This section is excerpted from @emph{The GNU C Library} reference manual"
3020
 
"by Sandra Loosemore with Richard M. Stallman, Roland McGrath, and Andrew"
3021
 
"Oram."
3022
 
""
3023
 
"The GNU C library supports the standard POSIX.2 interface.  Programs"
3024
 
"using this interface should include the header file @file{rxposix.h}."
3025
 
""
3026
 
"@menu"
3027
 
"* POSIX Regexp Compilation::    Using @code{regcomp} to prepare to match."
3028
 
"* Flags for POSIX Regexps::     Syntax variations for @code{regcomp}."
3029
 
"* Matching POSIX Regexps::      Using @code{regexec} to match the compiled"
3030
 
"                                  pattern that you get from @code{regcomp}."
3031
 
"* Regexp Subexpressions::       Finding which parts of the string were matched."
3032
 
"* Subexpression Complications::  Find points of which parts were matched."
3033
 
"* Regexp Cleanup::              Freeing storage; reporting errors."
3034
 
"@end menu"
3035
 
""
3036
 
"@node POSIX Regexp Compilation, Flags for POSIX Regexps, Posix Entry Points, Posix Entry Points"
3037
 
"@section POSIX Regular Expression Compilation"
3038
 
""
3039
 
"Before you can actually match a regular expression, you must"
3040
 
"@dfn{compile} it.  This is not true compilation---it produces a special"
3041
 
"data structure, not machine instructions.  But it is like ordinary"
3042
 
"compilation in that its purpose is to enable you to ``execute'' the"
3043
 
"pattern fast.  (@xref{Matching POSIX Regexps}, for how to use the"
3044
 
"compiled regular expression for matching.)"
3045
 
""
3046
 
"There is a special data type for compiled regular expressions:"
3047
 
""
3048
 
"@comment regex.h"
3049
 
"@comment POSIX.2"
3050
 
"@deftp {Data Type} regex_t"
3051
 
"This type of object holds a compiled regular expression."
3052
 
"It is actually a structure.  It has just one field that your programs"
3053
 
"should look at:"
3054
 
""
3055
 
"@table @code"
3056
 
"@item re_nsub"
3057
 
"This field holds the number of parenthetical subexpressions in the"
3058
 
"regular expression that was compiled."
3059
 
"@end table"
3060
 
""
3061
 
"There are several other fields, but we don't describe them here, because"
3062
 
"only the functions in the library should use them."
3063
 
"@end deftp"
3064
 
""
3065
 
"After you create a @code{regex_t} object, you can compile a regular"
3066
 
"expression into it by calling @code{regcomp}."
3067
 
""
3068
 
"@comment regex.h"
3069
 
"@comment POSIX.2"
3070
 
"@deftypefun int regcomp (regex_t *@var{compiled}, const char *@var{pattern}, int @var{cflags})"
3071
 
"@deftypefunx int regncomp (regex_t *@var{compiled}, int len, const char *@var{pattern}, int @var{cflags})"
3072
 
"The function @code{regcomp} ``compiles'' a regular expression into a"
3073
 
"data structure that you can use with @code{regexec} to match against a"
3074
 
"string.  The compiled regular expression format is designed for"
3075
 
"efficient matching.  @code{regcomp} stores it into @code{*@var{compiled}}."
3076
 
""
3077
 
"The parameter @var{pattern} points to the regular expression to be"
3078
 
"compiled.  When using @code{regcomp}, @var{pattern} must be"
3079
 
"0-terminated.  When using @code{regncomp}, @var{pattern} must be"
3080
 
"@var{len} characters long."
3081
 
""
3082
 
"@code{regncomp} is not a standard function;  strictly POSIX programs"
3083
 
"should avoid using it."
3084
 
""
3085
 
"It's up to you to allocate an object of type @code{regex_t} and pass its"
3086
 
"address to @code{regcomp}."
3087
 
""
3088
 
"The argument @var{cflags} lets you specify various options that control"
3089
 
"the syntax and semantics of regular expressions.  @xref{Flags for POSIX"
3090
 
"Regexps}."
3091
 
""
3092
 
"If you use the flag @code{REG_NOSUB}, then @code{regcomp} omits from"
3093
 
"the compiled regular expression the information necessary to record"
3094
 
"how subexpressions actually match.  In this case, you might as well"
3095
 
"pass @code{0} for the @var{matchptr} and @var{nmatch} arguments when"
3096
 
"you call @code{regexec}."
3097
 
""
3098
 
"If you don't use @code{REG_NOSUB}, then the compiled regular expression"
3099
 
"does have the capacity to record how subexpressions match.  Also,"
3100
 
"@code{regcomp} tells you how many subexpressions @var{pattern} has, by"
3101
 
"storing the number in @code{@var{compiled}->re_nsub}.  You can use that"
3102
 
"value to decide how long an array to allocate to hold information about"
3103
 
"subexpression matches."
3104
 
""
3105
 
"@code{regcomp} returns @code{0} if it succeeds in compiling the regular"
3106
 
"expression; otherwise, it returns a nonzero error code (see the table"
3107
 
"below).  You can use @code{regerror} to produce an error message string"
3108
 
"describing the reason for a nonzero value; see @ref{Regexp Cleanup}."
3109
 
""
3110
 
"@end deftypefun"
3111
 
""
3112
 
"Here are the possible nonzero values that @code{regcomp} can return:"
3113
 
""
3114
 
"@table @code"
3115
 
"@comment regex.h"
3116
 
"@comment POSIX.2"
3117
 
"@item REG_BADBR"
3118
 
"There was an invalid @samp{\\@{@dots{}\\@}} construct in the regular"
3119
 
"expression.  A valid @samp{\\@{@dots{}\\@}} construct must contain either"
3120
 
"a single number, or two numbers in increasing order separated by a"
3121
 
"comma."
3122
 
""
3123
 
"@comment regex.h"
3124
 
"@comment POSIX.2"
3125
 
"@item REG_BADPAT"
3126
 
"There was a syntax error in the regular expression."
3127
 
""
3128
 
"@comment regex.h"
3129
 
"@comment POSIX.2"
3130
 
"@item REG_BADRPT"
3131
 
"A repetition operator such as @samp{?} or @samp{*} appeared in a bad"
3132
 
"position (with no preceding subexpression to act on)."
3133
 
""
3134
 
"@comment regex.h"
3135
 
"@comment POSIX.2"
3136
 
"@item REG_ECOLLATE"
3137
 
"The regular expression referred to an invalid collating element (one not"
3138
 
"defined in the current locale for string collation).  "
3139
 
""
3140
 
"@comment regex.h"
3141
 
"@comment POSIX.2"
3142
 
"@item REG_ECTYPE"
3143
 
"The regular expression referred to an invalid character class name."
3144
 
""
3145
 
"@comment regex.h"
3146
 
"@comment POSIX.2"
3147
 
"@item REG_EESCAPE"
3148
 
"The regular expression ended with @samp{\\}."
3149
 
""
3150
 
"@comment regex.h"
3151
 
"@comment POSIX.2"
3152
 
"@item REG_ESUBREG"
3153
 
"There was an invalid number in the @samp{\\@var{digit}} construct."
3154
 
""
3155
 
"@comment regex.h"
3156
 
"@comment POSIX.2"
3157
 
"@item REG_EBRACK"
3158
 
"There were unbalanced square brackets in the regular expression."
3159
 
""
3160
 
"@comment regex.h"
3161
 
"@comment POSIX.2"
3162
 
"@item REG_EPAREN"
3163
 
"An extended regular expression had unbalanced parentheses,"
3164
 
"or a basic regular expression had unbalanced @samp{\\(} and @samp{\\)}."
3165
 
""
3166
 
"@comment regex.h"
3167
 
"@comment POSIX.2"
3168
 
"@item REG_EBRACE"
3169
 
"The regular expression had unbalanced @samp{\\@{} and @samp{\\@}}."
3170
 
""
3171
 
"@comment regex.h"
3172
 
"@comment POSIX.2"
3173
 
"@item REG_ERANGE"
3174
 
"One of the endpoints in a range expression was invalid."
3175
 
""
3176
 
"@comment regex.h"
3177
 
"@comment POSIX.2"
3178
 
"@item REG_ESPACE"
3179
 
"@code{regcomp} ran out of memory."
3180
 
"@end table"
3181
 
""
3182
 
"@node Flags for POSIX Regexps, Matching POSIX Regexps, POSIX Regexp Compilation, Posix Entry Points"
3183
 
"@section Flags for POSIX Regular Expressions"
3184
 
""
3185
 
"These are the bit flags that you can use in the @var{cflags} operand when"
3186
 
"compiling a regular expression with @code{regcomp}."
3187
 
" "
3188
 
"@table @code"
3189
 
"@comment regex.h"
3190
 
"@comment POSIX.2"
3191
 
"@item REG_EXTENDED"
3192
 
"Treat the pattern as an extended regular expression, rather than as a"
3193
 
"basic regular expression."
3194
 
""
3195
 
"@comment regex.h"
3196
 
"@comment POSIX.2"
3197
 
"@item REG_ICASE"
3198
 
"Ignore case when matching letters."
3199
 
""
3200
 
"@comment regex.h"
3201
 
"@comment POSIX.2"
3202
 
"@item REG_NOSUB"
3203
 
"Don't bother storing the contents of the @var{matches-ptr} array."
3204
 
""
3205
 
"@comment regex.h"
3206
 
"@comment POSIX.2"
3207
 
"@item REG_NEWLINE"
3208
 
"Treat a newline in @var{string} as dividing @var{string} into multiple"
3209
 
"lines, so that @samp{$} can match before the newline and @samp{^} can"
3210
 
"match after.  Also, don't permit @samp{.} to match a newline, and don't"
3211
 
"permit @samp{[^@dots{}]} to match a newline."
3212
 
""
3213
 
"Otherwise, newline acts like any other ordinary character."
3214
 
"@end table"
3215
 
""
3216
 
"@node Matching POSIX Regexps, Regexp Subexpressions, Flags for POSIX Regexps, Posix Entry Points"
3217
 
"@section Matching a Compiled POSIX Regular Expression"
3218
 
""
3219
 
"Once you have compiled a regular expression, as described in @ref{POSIX"
3220
 
"Regexp Compilation}, you can match it against strings using"
3221
 
"@code{regexec}.  A match anywhere inside the string counts as success,"
3222
 
"unless the regular expression contains anchor characters (@samp{^} or"
3223
 
"@samp{$})."
3224
 
""
3225
 
"@comment regex.h"
3226
 
"@comment POSIX.2"
3227
 
"@deftypefun int regexec (regex_t *@var{compiled}, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
3228
 
"@deftypefunx int regnexec (regex_t *@var{compiled}, int len, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
3229
 
"This function tries to match the compiled regular expression"
3230
 
"@code{*@var{compiled}} against @var{string}."
3231
 
""
3232
 
"@code{regexec} returns @code{0} if the regular expression matches;"
3233
 
"otherwise, it returns a nonzero value.  See the table below for"
3234
 
"what nonzero values mean.  You can use @code{regerror} to produce an"
3235
 
"error message string describing the reason for a nonzero value; "
3236
 
"see @ref{Regexp Cleanup}."
3237
 
""
3238
 
"The parameter @var{string} points to the text to search.  When using"
3239
 
"@code{regexec}, @var{string} must be 0-terminated.  When using"
3240
 
"@code{regnexec}, @var{string} must be @var{len} characters long."
3241
 
""
3242
 
"@code{regnexec} is not a standard function;  strictly POSIX programs"
3243
 
"should avoid using it."
3244
 
""
3245
 
"The argument @var{eflags} is a word of bit flags that enable various"
3246
 
"options."
3247
 
""
3248
 
"If you want to get information about what part of @var{string} actually"
3249
 
"matched the regular expression or its subexpressions, use the arguments"
3250
 
"@var{matchptr} and @var{nmatch}.  Otherwise, pass @code{0} for "
3251
 
"@var{nmatch}, and @code{NULL} for @var{matchptr}.  @xref{Regexp"
3252
 
"Subexpressions}."
3253
 
"@end deftypefun"
3254
 
""
3255
 
"You must match the regular expression with the same set of current"
3256
 
"locales that were in effect when you compiled the regular expression."
3257
 
""
3258
 
"The function @code{regexec} accepts the following flags in the"
3259
 
"@var{eflags} argument:"
3260
 
""
3261
 
"@table @code "
3262
 
"@comment regex.h"
3263
 
"@comment POSIX.2"
3264
 
"@item REG_NOTBOL"
3265
 
"Do not regard the beginning of the specified string as the beginning of"
3266
 
"a line; more generally, don't make any assumptions about what text might"
3267
 
"precede it."
3268
 
""
3269
 
"@comment regex.h"
3270
 
"@comment POSIX.2"
3271
 
"@item REG_NOTEOL"
3272
 
"Do not regard the end of the specified string as the end of a line; more"
3273
 
"generally, don't make any assumptions about what text might follow it."
3274
 
"@end table"
3275
 
""
3276
 
"Here are the possible nonzero values that @code{regexec} can return:"
3277
 
""
3278
 
"@table @code"
3279
 
"@comment regex.h"
3280
 
"@comment POSIX.2"
3281
 
"@item REG_NOMATCH"
3282
 
"The pattern didn't match the string.  This isn't really an error."
3283
 
""
3284
 
"@comment regex.h"
3285
 
"@comment POSIX.2"
3286
 
"@item REG_ESPACE"
3287
 
"@code{regexec} ran out of memory."
3288
 
"@end table"
3289
 
""
3290
 
"@node Regexp Subexpressions, Subexpression Complications, Matching POSIX Regexps, Posix Entry Points"
3291
 
"@section Match Results with Subexpressions"
3292
 
""
3293
 
"When @code{regexec} matches parenthetical subexpressions of"
3294
 
"@var{pattern}, it records which parts of @var{string} they match.  It"
3295
 
"returns that information by storing the offsets into an array whose"
3296
 
"elements are structures of type @code{regmatch_t}.  The first element of"
3297
 
"the array (index @code{0}) records the part of the string that matched"
3298
 
"the entire regular expression.  Each other element of the array records"
3299
 
"the beginning and end of the part that matched a single parenthetical"
3300
 
"subexpression."
3301
 
""
3302
 
"@comment regex.h"
3303
 
"@comment POSIX.2"
3304
 
"@deftp {Data Type} regmatch_t"
3305
 
"This is the data type of the @var{matcharray} array that you pass to"
3306
 
"@code{regexec}.  It containes two structure fields, as follows:"
3307
 
""
3308
 
"@table @code"
3309
 
"@item rm_so"
3310
 
"The offset in @var{string} of the beginning of a substring.  Add this"
3311
 
"value to @var{string} to get the address of that part."
3312
 
""
3313
 
"@item rm_eo"
3314
 
"The offset in @var{string} of the end of the substring."
3315
 
"@end table"
3316
 
"@end deftp"
3317
 
""
3318
 
"@comment regex.h"
3319
 
"@comment POSIX.2"
3320
 
"@deftp {Data Type} regoff_t"
3321
 
"@code{regoff_t} is an alias for another signed integer type."
3322
 
"The fields of @code{regmatch_t} have type @code{regoff_t}."
3323
 
"@end deftp"
3324
 
""
3325
 
"The @code{regmatch_t} elements correspond to subexpressions"
3326
 
"positionally; the first element (index @code{1}) records where the first"
3327
 
"subexpression matched, the second element records the second"
3328
 
"subexpression, and so on.  The order of the subexpressions is the order"
3329
 
"in which they begin."
3330
 
""
3331
 
"When you call @code{regexec}, you specify how long the @var{matchptr}"
3332
 
"array is, with the @var{nmatch} argument.  This tells @code{regexec} how"
3333
 
"many elements to store.  If the actual regular expression has more than"
3334
 
"@var{nmatch} subexpressions, then you won't get offset information about"
3335
 
"the rest of them.  But this doesn't alter whether the pattern matches a"
3336
 
"particular string or not."
3337
 
""
3338
 
"If you don't want @code{regexec} to return any information about where"
3339
 
"the subexpressions matched, you can either supply @code{0} for"
3340
 
"@var{nmatch}, or use the flag @code{REG_NOSUB} when you compile the"
3341
 
"pattern with @code{regcomp}."
3342
 
""
3343
 
"@node Subexpression Complications, Regexp Cleanup, Regexp Subexpressions, Posix Entry Points"
3344
 
"@section Complications in Subexpression Matching"
3345
 
""
3346
 
"Sometimes a subexpression matches a substring of no characters.  This"
3347
 
"happens when @samp{f\\(o*\\)} matches the string @samp{fum}.  (It really"
3348
 
"matches just the @samp{f}.)  In this case, both of the offsets identify"
3349
 
"the point in the string where the null substring was found.  In this"
3350
 
"example, the offsets are both @code{1}."
3351
 
""
3352
 
"Sometimes the entire regular expression can match without using some of"
3353
 
"its subexpressions at all---for example, when @samp{ba\\(na\\)*} matches the"
3354
 
"string @samp{ba}, the parenthetical subexpression is not used.  When"
3355
 
"this happens, @code{regexec} stores @code{-1} in both fields of the"
3356
 
"element for that subexpression."
3357
 
""
3358
 
"Sometimes matching the entire regular expression can match a particular"
3359
 
"subexpression more than once---for example, when @samp{ba\\(na\\)*}"
3360
 
"matches the string @samp{bananana}, the parenthetical subexpression"
3361
 
"matches three times.  When this happens, @code{regexec} usually stores"
3362
 
"the offsets of the last part of the string that matched the"
3363
 
"subexpression.  In the case of @samp{bananana}, these offsets are"
3364
 
"@code{6} and @code{8}."
3365
 
""
3366
 
"But the last match is not always the one that is chosen.  It's more"
3367
 
"accurate to say that the last @emph{opportunity} to match is the one"
3368
 
"that takes precedence.  What this means is that when one subexpression"
3369
 
"appears within another, then the results reported for the inner"
3370
 
"subexpression reflect whatever happened on the last match of the outer"
3371
 
"subexpression.  For an example, consider @samp{\\(ba\\(na\\)*s \\)*} matching"
3372
 
"the string @samp{bananas bas }.  The last time the inner expression"
3373
 
"actually matches is near the end of the first word.  But it is "
3374
 
"@emph{considered} again in the second word, and fails to match there."
3375
 
"@code{regexec} reports nonuse of the ``na'' subexpression."
3376
 
""
3377
 
"Another place where this rule applies is when the regular expression"
3378
 
"@w{@samp{\\(ba\\(na\\)*s \\|nefer\\(ti\\)* \\)*}} matches @samp{bananas nefertiti}."
3379
 
"The ``na'' subexpression does match in the first word, but it doesn't"
3380
 
"match in the second word because the other alternative is used there."
3381
 
"Once again, the second repetition of the outer subexpression overrides"
3382
 
"the first, and within that second repetition, the ``na'' subexpression"
3383
 
"is not used.  So @code{regexec} reports nonuse of the ``na''"
3384
 
"subexpression."
3385
 
""
3386
 
"@node Regexp Cleanup,  , Subexpression Complications, Posix Entry Points"
3387
 
"@section POSIX Regexp Matching Cleanup"
3388
 
""
3389
 
"When you are finished using a compiled regular expression, you can"
3390
 
"free the storage it uses by calling @code{regfree}."
3391
 
""
3392
 
"@comment regex.h"
3393
 
"@comment POSIX.2"
3394
 
"@deftypefun void regfree (regex_t *@var{compiled})"
3395
 
"Calling @code{regfree} frees all the storage that @code{*@var{compiled}}"
3396
 
"points to.  This includes various internal fields of the @code{regex_t}"
3397
 
"structure that aren't documented in this manual."
3398
 
""
3399
 
"@code{regfree} does not free the object @code{*@var{compiled}} itself."
3400
 
"@end deftypefun"
3401
 
""
3402
 
"You should always free the space in a @code{regex_t} structure with"
3403
 
"@code{regfree} before using the structure to compile another regular"
3404
 
"expression."
3405
 
""
3406
 
"When @code{regcomp} or @code{regexec} reports an error, you can use"
3407
 
"the function @code{regerror} to turn it into an error message string."
3408
 
""
3409
 
"@comment regex.h"
3410
 
"@comment POSIX.2"
3411
 
"@deftypefun size_t regerror (int @var{errcode}, regex_t *@var{compiled}, char *@var{buffer}, size_t @var{length})"
3412
 
"This function produces an error message string for the error code"
3413
 
"@var{errcode}, and stores the string in @var{length} bytes of memory"
3414
 
"starting at @var{buffer}.  For the @var{compiled} argument, supply the"
3415
 
"same compiled regular expression structure that @code{regcomp} or"
3416
 
"@code{regexec} was working with when it got the error.  Alternatively,"
3417
 
"you can supply @code{NULL} for @var{compiled}; you will still get a"
3418
 
"meaningful error message, but it might not be as detailed."
3419
 
""
3420
 
"If the error message can't fit in @var{length} bytes (including a"
3421
 
"terminating null character), then @code{regerror} truncates it."
3422
 
"The string that @code{regerror} stores is always null-terminated"
3423
 
"even if it has been truncated."
3424
 
""
3425
 
"The return value of @code{regerror} is the minimum length needed to"
3426
 
"store the entire error message.  If this is less than @var{length}, then"
3427
 
"the error message was not truncated, and you can use it.  Otherwise, you"
3428
 
"should call @code{regerror} again with a larger buffer."
3429
 
""
3430
 
"Here is a function which uses @code{regerror}, but always dynamically"
3431
 
"allocates a buffer for the error message:"
3432
 
""
3433
 
"@smallexample"
3434
 
"char *get_regerror (int errcode, regex_t *compiled)"
3435
 
"@{"
3436
 
"  size_t length = regerror (errcode, compiled, NULL, 0);"
3437
 
"  char *buffer = xmalloc (length);"
3438
 
"  (void) regerror (errcode, compiled, buffer, length);"
3439
 
"  return buffer;"
3440
 
"@}"
3441
 
"@end smallexample"
3442
 
"@end deftypefun"
3443
 
""
3444
 
"@node  Beyond POSIX, Rx Theory, Posix Entry Points, Top"
3445
 
"@chapter Beyond POSIX"
3446
 
""
3447
 
"This section is not finished documentation, but rather a collection of"
3448
 
"pointers towards some of the interesting, non-standard features of Rx."
3449
 
""
3450
 
"@section New Regexp Operators"
3451
 
""
3452
 
"Rx supports some unusual regexp syntax."
3453
 
""
3454
 
"@code{[[:cut N:]]} sets @code{pmatch[0].final_tag} to N and causes the"
3455
 
"matching to stop instantly.  If N is 0, the overall match fails,"
3456
 
"otherwise it succeeds."
3457
 
""
3458
 
"@code{[[:(:]] ... [[:):]]} is just like @code{\\( ... \\)} except that in"
3459
 
"the first case, no pmatch entries are changed, and the subexpression is"
3460
 
"not counted in the numbering of parenthesized subexpressions."
3461
 
""
3462
 
"@code{[[:(:]] ... [[:):]]} can be used when you do not need to know"
3463
 
"where a subexpression matched but are only using parentheses to effect"
3464
 
"the parsing of the regexp.  "
3465
 
""
3466
 
"There are two reasons to use @code{[[:(:]] ... [[:):]]}:"
3467
 
""
3468
 
"1. regexec will run faster."
3469
 
""
3470
 
"2. Currently, only 8 backreferencable subexpressions are supported:"
3471
 
"@code{\\1 .. \\9}.  Using @code{[[:(:]] ... [[:):]]} is a way to conserve"
3472
 
"backreferencable subexpression names in an expression with many"
3473
 
"parentheses."
3474
 
""
3475
 
"@section New POSIX Functions"
3476
 
""
3477
 
"@code{regncomp} and @code{regnexec} are non-standard generalizations of"
3478
 
"@code{regcomp} and @code{regexec}."
3479
 
""
3480
 
"@section Tuning POSIX performance"
3481
 
""
3482
 
"Two mysterious parmaters can be used to trade-off performance and"
3483
 
"memory use."
3484
 
""
3485
 
"At compile-time they are @code{RX_DEFAULT_DFA_CACHE_SIZE} and"
3486
 
"@code{RX_DEFAULT_NFA_DELAY}.  "
3487
 
""
3488
 
"If you want to mess with these (I generally don't advise it), I suggest"
3489
 
"experimenting for your particular application/memory situation; frob"
3490
 
"these by powers of two and try out the results on what you expect will"
3491
 
"be typical regexp workloads."
3492
 
""
3493
 
"You can also set those parameters at run-time (before calling any regexp"
3494
 
"functions) by tweaking the corresponding variables:"
3495
 
""
3496
 
"@code{rx_default_cache->bytes_allowed}"
3497
 
""
3498
 
"and"
3499
 
""
3500
 
"@code{rx_basic_unfaniverse_delay}"
3501
 
""
3502
 
""
3503
 
""
3504
 
"@section POSIX stream-style interface"
3505
 
""
3506
 
"@code{rx_make_solutions}, @code{rx_next_solution}, and"
3507
 
"@code{rx_free_solutions} are a lower level alternative to the posix"
3508
 
"functions.  Using those functions, you can compare a compiled regexp to"
3509
 
"a string that is not contiguous in memory or even a string that is not"
3510
 
"entirely in memory at any one time."
3511
 
""
3512
 
"The code in rxposix.c points out how those functions are used."
3513
 
""
3514
 
""
3515
 
"@section DFAs Directly"
3516
 
""
3517
 
"If you are only interested in pure regular expressions (no pmatch data,"
3518
 
"no backreferences, and no counted subexpressions), you can parse a"
3519
 
"regexp using @code{rx_parse}, convert it to an nfa using @code{rx_unfa},"
3520
 
"and run the dfa using @code{rx_init_system}, @code{rx_advance_to_final},"
3521
 
"and @code{rx_terminate_system}.  The dfa Scheme primitives in"
3522
 
"@file{rgx.c} may provide some guide."
3523
 
""
3524
 
"@node    Rx Theory,  , Beyond POSIX, Top"
3525
 
"@chapter Rx Theory"
3526
 
"@cindex Rx Theory"
3527
 
""
3528
 
"There are two match algorithms.  One is for truly regular regexps (those"
3529
 
"that can be reduced to a dfa).  The other is for non-regular regexps."
3530
 
""
3531
 
"The dfa algorithm implements the idea suggested in @cite{Compilers} by"
3532
 
"Aho, Sethi and Ullman:"
3533
 
""
3534
 
"@quotation"
3535
 
"[One] approach [to pattern matching regular expressions] is to use a"
3536
 
"DFA, but avoid constructing all of the transition table by using a"
3537
 
"technique called \"lazy transition evaluation\".  Here, transitions are"
3538
 
"computed at run time [when] actually needed. [T]ransitions are"
3539
 
"stored in a cache. [....] If the cache becomes full, we can erase some"
3540
 
"previously computed transition to make room for the new transition."
3541
 
"@end quotation"
3542
 
""
3543
 
"The implementation in Rx is generalized from that, but the above"
3544
 
"description covers what is used for Posix patterns. "
3545
 
""
3546
 
"The non-dfa algorithm implements a \"recursive decomposition\" technique"
3547
 
"described in email by Henry Spencer.  For a given pattern, this"
3548
 
"algorithm first checks to see if a simpler, superset language,"
3549
 
"DFA-pattern matches.  If it does, then this algorithm does the"
3550
 
"detail-work to see if the non-DFA pattern matches."
3551
 
"\\input texinfo   @c -*-texinfo-*-"
3552
 
"@c %**start of header"
3553
 
"@setfilename rx.info"
3554
 
"@settitle Rx"
3555
 
"@setchapternewpage odd"
3556
 
"@c %**end of header"
3557
 
""
3558
 
"@c  This title page illustrates only one of the"
3559
 
"@c  two methods of forming a title page."
3560
 
""
3561
 
"@titlepage"
3562
 
"@title Rx"
3563
 
"@author Tom Lord"
3564
 
""
3565
 
"@center except the chapter \"Posix Entry Points\" "
3566
 
"@center from @emph{The GNU C Library} reference manual"
3567
 
"@center by Sandra Loosemore"
3568
 
"@center with"
3569
 
"@center Richard M. Stallman, Roland McGrath, and Andrew Oram"
3570
 
""
3571
 
"@c  The following two commands"
3572
 
"@c  start the copyright page."
3573
 
"@page"
3574
 
"@vskip 0pt plus 1filll"
3575
 
"Copyright @copyright{} 1995 Cygnus Support"
3576
 
""
3577
 
"except the chapter \"Posix Entry Points\" which is:"
3578
 
""
3579
 
"Copyright @copyright{} 1995 Free Software Foundation, Inc."
3580
 
""
3581
 
"Permission is granted to make and distribute verbatim copies of"
3582
 
"this manual provided the copyright notice and this permission notice"
3583
 
"are preserved on all copies."
3584
 
""
3585
 
"Permission is granted to copy and distribute modified versions of this"
3586
 
"manual under the conditions for verbatim copying, provided that the entire"
3587
 
"resulting derived work is distributed under the terms of a permission"
3588
 
"notice identical to this one."
3589
 
""
3590
 
"Permission is granted to copy and distribute translations of this manual"
3591
 
"into another language, under the above conditions for modified versions,"
3592
 
"except that this permission notice may be stated in a translation approved"
3593
 
"by the author."
3594
 
""
3595
 
"@end titlepage"
3596
 
""
3597
 
"@node Top, Copying, (dir), (dir)"
3598
 
"@top Rx"
3599
 
""
3600
 
"This document describes Rx."
3601
 
""
3602
 
"@menu"
3603
 
"* Copying::                            Sharing is good."
3604
 
"* Overview::                           Fnord"
3605
 
"* Posix Basic Regular Expressions::    A popular regexp syntax."
3606
 
"* Posix Entry Points::                 The POSIX way to regexp."
3607
 
"* Beyond POSIX::                       Hints about cool features."
3608
 
"* Rx Theory::                          Hints about how it works."
3609
 
"@end menu"
3610
 
""
3611
 
"@node Copying, Overview, Top, Top"
3612
 
"@chapter Copying"
3613
 
""
3614
 
"@center Copyright (C) 1996"
3615
 
"@center Tom Lord"
3616
 
"@center Berkeley, CA USA"
3617
 
""
3618
 
"@center except portions of \"POSIX Regex Functions\" which are"
3619
 
"@center Copyright (C) 1995"
3620
 
"@center Free Software Foundation, Inc."
3621
 
""
3622
 
"@noindent"
3623
 
"Permission to use, copy, modify, distribute, and sell this software and"
3624
 
"its documentation for any purpose is hereby granted without fee,"
3625
 
"provided that the above copyright notice appear in all copies and that"
3626
 
"both that copyright notice and this permission notice appear in"
3627
 
"supporting documentation."
3628
 
""
3629
 
"@center NO WARRANTY"
3630
 
""
3631
 
"@noindent"
3632
 
"BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR"
3633
 
"THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN"
3634
 
"OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES"
3635
 
"PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER"
3636
 
"EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED"
3637
 
"WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE"
3638
 
"ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH"
3639
 
"YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL"
3640
 
"NECESSARY SERVICING, REPAIR OR CORRECTION."
3641
 
""
3642
 
"@noindent"
3643
 
"IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING"
3644
 
"WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR"
3645
 
"REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR"
3646
 
"DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL"
3647
 
"DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM"
3648
 
"(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED"
3649
 
"INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF"
3650
 
"THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR"
3651
 
"OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES."
3652
 
""
3653
 
"@node  Overview, Posix Basic Regular Expressions, Copying, Top"
3654
 
"@chapter Overview"
3655
 
""
3656
 
"Nothing to say here, yet."
3657
 
""
3658
 
"@node  Posix Basic Regular Expressions, Posix Entry Points, Overview, Top"
3659
 
"@chapter Posix Basic Regular Expressions"
3660
 
""
3661
 
"The Posix Basic Regular Expression language is a notation for describing"
3662
 
"textual patterns.  Regexps are typically used by comparing them to a"
3663
 
"string to see if that string matches the pattern, or by searching within"
3664
 
"a string for a substring that matches."
3665
 
""
3666
 
"This chapter introduces the Posix regexp notation.  This is not a formal"
3667
 
"or precise definition of Posix regexps -- it is an intuitive and"
3668
 
"hopefully expository description of them."
3669
 
""
3670
 
"@menu"
3671
 
"* An Introduction to Regexps::  "
3672
 
"* Literal Regexps::             "
3673
 
"* Character Sets::              "
3674
 
"* Subexpressions::              "
3675
 
"* Repeated Subexpressions::     "
3676
 
"* Optional Subexpressions::     "
3677
 
"* Counted Subexpressions::      "
3678
 
"* Alternative Subexpressions::  "
3679
 
"* Backreferences::              "
3680
 
"* A Summary of Regexp Syntax::  "
3681
 
"* Ambiguous Patterns::          "
3682
 
"@end menu"
3683
 
""
3684
 
""
3685
 
"@node  An Introduction to Regexps, Literal Regexps, Posix Basic Regular Expressions, Posix Basic Regular Expressions"
3686
 
"@section An Introduction to Regexps"
3687
 
""
3688
 
"In the simplest cases, a regexp is just a literal string that must"
3689
 
"match exactly.   For example, the pattern:"
3690
 
""
3691
 
"@example"
3692
 
"regexp"
3693
 
"@end example"
3694
 
""
3695
 
"matches the string \"regexp\" and no others."
3696
 
""
3697
 
"Some characters have a special meaning when they occur in a regexp."
3698
 
"They aren't matched literally as in the previous example, but instead"
3699
 
"denote a more general pattern.   For example, the character @code{*}"
3700
 
"is used to indicate that the preceeding element of a regexp may be"
3701
 
"repeated 0, 1, or more times.  In the pattern:"
3702
 
""
3703
 
"@example"
3704
 
"smooo*th"
3705
 
"@end example"
3706
 
""
3707
 
"the @code{*} indicates that the preceeding @code{o} can be repeated 0 or"
3708
 
"more times.   So the pattern matches:"
3709
 
""
3710
 
"@example"
3711
 
"smooth"
3712
 
"smoooth"
3713
 
"smooooth"
3714
 
"smoooooth"
3715
 
"..."
3716
 
"@end example"
3717
 
""
3718
 
"Suppose you want to write a pattern that literally matches a special"
3719
 
"character like @code{*} -- in other words, you don't want to @code{*} to"
3720
 
"indicate a permissible repetition, but to match @code{*} literally.  This"
3721
 
"is accomplished by quoting the special character with a backslash.  "
3722
 
"The pattern:"
3723
 
""
3724
 
"@example"
3725
 
"smoo\\*th"
3726
 
"@end example"
3727
 
""
3728
 
"matches the string:"
3729
 
""
3730
 
"@example"
3731
 
"smoo*th"
3732
 
"@end example"
3733
 
""
3734
 
""
3735
 
"and no other strings."
3736
 
""
3737
 
"In five cases, the pattern is reversed -- a backslash makes the"
3738
 
"character special instead of making a special character normal.  The"
3739
 
"characters @code{+}, @code{?}, @code{|}, @code{(}, and @code{)} are"
3740
 
"normal but the sequences @code{\\+}, @code{\\?}, @code{\\|}, @code{\\(}, and"
3741
 
"@code{\\)} are special (their meaning is described later)."
3742
 
""
3743
 
"The remaining sections of this chapter introduce and explain the various"
3744
 
"special characters that can occur in regexps."
3745
 
""
3746
 
"@node  Literal Regexps, Character Sets, An Introduction to Regexps, Posix Basic Regular Expressions"
3747
 
"@section Literal Regexps"
3748
 
""
3749
 
"A literal regexp is a string which contains no special characters."
3750
 
"A literal regexp matches an identical string, but no other characters."
3751
 
"For example:"
3752
 
""
3753
 
"@example"
3754
 
"literally"
3755
 
"@end example"
3756
 
""
3757
 
"matches"
3758
 
""
3759
 
"@example"
3760
 
"literally"
3761
 
"@end example"
3762
 
""
3763
 
"and nothing else."
3764
 
""
3765
 
"Generally, whitespace characters, numbers, and letters are not special."
3766
 
"Some punctuation characters are special and some are not (the syntax"
3767
 
"summary at the end of this chapter makes a convenient reference for"
3768
 
"which characters are special and which aren't)."
3769
 
""
3770
 
""
3771
 
"@node  Character Sets, Subexpressions, Literal Regexps, Posix Basic Regular Expressions"
3772
 
"@section Character Sets"
3773
 
""
3774
 
"This section introduces the special characters @code{.} and @code{[}."
3775
 
""
3776
 
"@code{.} matches any character except the NULL character.  For example:"
3777
 
""
3778
 
"@example"
3779
 
"p.ck"
3780
 
"@end example"
3781
 
""
3782
 
"matches"
3783
 
""
3784
 
"@example"
3785
 
"pick"
3786
 
"pack"
3787
 
"puck"
3788
 
"pbck"
3789
 
"pcck"
3790
 
"p.ck"
3791
 
""
3792
 
"..."
3793
 
"@end example"
3794
 
""
3795
 
""
3796
 
"@code{[} begins a @dfn{character set}.  A character set is similar to"
3797
 
"@code{.} in that it matches not a single, literal character, but any"
3798
 
"of a set of characters.   @code{[} is different from @code{.} in that"
3799
 
"with @code{[}, you define the set of characters explicitly."
3800
 
""
3801
 
"There are three basic forms a character set can take."
3802
 
""
3803
 
"In the first form, the character set is spelled out:"
3804
 
""
3805
 
"@example"
3806
 
"[<cset-spec>]  -- every character in <cset-spec> is in the set."
3807
 
"@end example"
3808
 
""
3809
 
"In the second form, the character set indicated is the negation of"
3810
 
"a character set is explicitly spelled out:"
3811
 
""
3812
 
"@example"
3813
 
"[^<cset-spec>] -- every character *not* in <cset-spec> is in the set."
3814
 
"@end example"
3815
 
""
3816
 
"A @code{<cset-spec>} is more or less an explicit enumeration of a set"
3817
 
"of characters.  It can be written as a string of individual characters:"
3818
 
""
3819
 
"@example"
3820
 
"[aeiou]"
3821
 
"@end example"
3822
 
""
3823
 
"or as a range of characters:"
3824
 
""
3825
 
"@example"
3826
 
"[0-9]"
3827
 
"@end example"
3828
 
""
3829
 
"These two forms can be mixed:"
3830
 
""
3831
 
"@example"
3832
 
"[A-za-z0-9_$]"
3833
 
"@end example"
3834
 
""
3835
 
"Note that special regexp characters (such as @code{*}) are @emph{not}"
3836
 
"special within a character set.  @code{-}, as illustrated above,"
3837
 
"@emph{is} special, except, as illustrated below, when it is the first"
3838
 
"character mentioned."
3839
 
""
3840
 
"This is a four-character set:"
3841
 
""
3842
 
"@example"
3843
 
"[-+*/]"
3844
 
"@end example"
3845
 
""
3846
 
"The third form of a character set makes use of a pre-defined \"character"
3847
 
"class\":"
3848
 
""
3849
 
"@example"
3850
 
"[[:class-name:]] -- every character described by class-name is in the set."
3851
 
"@end example"
3852
 
""
3853
 
"The supported character classes are:"
3854
 
""
3855
 
"@example"
3856
 
"alnum  - the set of alpha-numeric characters"
3857
 
"alpha  - the set of alphabetic characters"
3858
 
"blank  - tab and space"
3859
 
"cntrl  - the control characters"
3860
 
"digit  - decimal digits"
3861
 
"graph  - all printable characters except space"
3862
 
"lower  - lower case letters"
3863
 
"print  - the \"printable\" characters"
3864
 
"punct  - punctuation"
3865
 
"space  - whitespace characters"
3866
 
"upper  - upper case letters"
3867
 
"xdigit - hexidecimal digits"
3868
 
"@end example"
3869
 
""
3870
 
"Finally, character class sets can also be inverted:"
3871
 
""
3872
 
"@example"
3873
 
"[^[:space:]] - all non-whitespace characters"
3874
 
"@end example"
3875
 
""
3876
 
"Character sets can be used in a regular expression anywhere a literal"
3877
 
"character can."
3878
 
""
3879
 
"@node  Subexpressions, Repeated Subexpressions, Character Sets, Posix Basic Regular Expressions"
3880
 
"@section Subexpressions"
3881
 
""
3882
 
"A subexpression is a regular expression enclosed in @code{\\(} and"
3883
 
"@code{\\)}.  A subexpression can be used anywhere a single character or"
3884
 
"character set can be used."
3885
 
""
3886
 
"Subexpressions are useful for grouping regexp constructs.  For example,"
3887
 
"the repeat operator, @code{*}, usually applies to just the preceeding"
3888
 
"character.   Recall that:"
3889
 
""
3890
 
"@example"
3891
 
"smooo*th"
3892
 
"@end example"
3893
 
""
3894
 
"matches"
3895
 
""
3896
 
"@example"
3897
 
"smooth"
3898
 
"smoooth"
3899
 
"..."
3900
 
"@end example"
3901
 
""
3902
 
""
3903
 
"Using a subexpression, we can apply @code{*} to a longer string:"
3904
 
""
3905
 
"@example"
3906
 
"banan\\(an\\)*a"
3907
 
"@end example"
3908
 
""
3909
 
"matches"
3910
 
""
3911
 
"@example"
3912
 
"banana"
3913
 
"bananana"
3914
 
"banananana"
3915
 
"..."
3916
 
"@end example"
3917
 
""
3918
 
"Subexpressions also have a special meaning with regard to backreferences"
3919
 
"and substitutions (see @xref{Backreferences})."
3920
 
""
3921
 
""
3922
 
"@node  Repeated Subexpressions, Optional Subexpressions, Subexpressions, Posix Basic Regular Expressions"
3923
 
"@section Repeated Subexpressions"
3924
 
""
3925
 
""
3926
 
"@code{*} is the repeat operator.  It applies to the preceeding"
3927
 
"character, character set, subexpression or backreference.  It indicates"
3928
 
"that the preceeding element can be matched 0 or more times:"
3929
 
""
3930
 
"@example"
3931
 
"bana\\(na\\)*"
3932
 
"@end example"
3933
 
""
3934
 
"matches"
3935
 
""
3936
 
"@example"
3937
 
"bana"
3938
 
"banana"
3939
 
"bananana"
3940
 
"banananana"
3941
 
"..."
3942
 
"@end example"
3943
 
""
3944
 
""
3945
 
""
3946
 
"@code{\\+} is similar to @code{*} except that @code{\\+} requires the"
3947
 
"preceeding element to be matched at least once.  So while:"
3948
 
""
3949
 
"@example"
3950
 
"bana\\(na\\)*"
3951
 
"@end example"
3952
 
""
3953
 
"matches"
3954
 
""
3955
 
"@example"
3956
 
"bana"
3957
 
"@end example"
3958
 
""
3959
 
"@example"
3960
 
"bana(na\\)\\+"
3961
 
"@end example"
3962
 
""
3963
 
"does not.   Both match "
3964
 
""
3965
 
"@example"
3966
 
"banana"
3967
 
"bananana"
3968
 
"banananana"
3969
 
"..."
3970
 
"@end example"
3971
 
""
3972
 
"Thus, @code{bana\\(na\\)+} is short-hand for @code{banana\\(na\\)*}."
3973
 
""
3974
 
""
3975
 
"@node  Optional Subexpressions, Counted Subexpressions, Repeated Subexpressions, Posix Basic Regular Expressions"
3976
 
"@section Optional Subexpressions"
3977
 
""
3978
 
"@code{\\?} indicates that the preceeding character, character set, or"
3979
 
"subexpression is optional.  It is permitted to match, or to be skipped:"
3980
 
""
3981
 
"@example"
3982
 
"CSNY\\?"
3983
 
"@end example"
3984
 
""
3985
 
"matches both"
3986
 
""
3987
 
"@example"
3988
 
"CSN"
3989
 
"@end example"
3990
 
""
3991
 
"and "
3992
 
""
3993
 
"@example"
3994
 
"CSNY"
3995
 
"@end example"
3996
 
""
3997
 
""
3998
 
"@node  Counted Subexpressions, Alternative Subexpressions, Optional Subexpressions, Posix Basic Regular Expressions"
3999
 
"@section Counted Subexpressions"
4000
 
""
4001
 
"An interval expression, @code{@{m,n@}} where @code{m} and @code{n} are"
4002
 
"non-negative integers with @code{n >= m}, applies to the preceeding"
4003
 
"character, character set, subexpression or backreference.  It indicates"
4004
 
"that the preceeding element must match at least @code{m} times and may"
4005
 
"match as many as @code{n} times."
4006
 
""
4007
 
"For example:"
4008
 
""
4009
 
"@example"
4010
 
"c\\([ad]\\)@{1,4@}"
4011
 
"@end example"
4012
 
""
4013
 
"matches"
4014
 
""
4015
 
"@example"
4016
 
"car"
4017
 
"cdr"
4018
 
"caar"
4019
 
"cdar"
4020
 
"..."
4021
 
"caaar"
4022
 
"cdaar"
4023
 
"..."
4024
 
"cadddr"
4025
 
"cddddr"
4026
 
"@end example"
4027
 
""
4028
 
""
4029
 
""
4030
 
"@node  Alternative Subexpressions, Backreferences, Counted Subexpressions, Posix Basic Regular Expressions"
4031
 
"@section Alternative Subexpressions"
4032
 
""
4033
 
"An alternative is written:"
4034
 
""
4035
 
"@example"
4036
 
"regexp-1\\|regexp-2\\|regexp-3\\|..."
4037
 
"@end example"
4038
 
""
4039
 
"It matches anything matched by some @code{regexp-n}.  For example:"
4040
 
""
4041
 
"@example"
4042
 
"Crosby, Stills, \\(and Nash\\|Nash, and Young\\)"
4043
 
"@end example"
4044
 
""
4045
 
"matches"
4046
 
""
4047
 
"@example"
4048
 
"Crosby, Stills, and Nash"
4049
 
"@end example"
4050
 
""
4051
 
"and"
4052
 
""
4053
 
"@example"
4054
 
"Crosby, Stills, Nash, and Young"
4055
 
"@end example"
4056
 
""
4057
 
""
4058
 
"@node  Backreferences, A Summary of Regexp Syntax, Alternative Subexpressions, Posix Basic Regular Expressions"
4059
 
"@section Backreferences, Extractions and Substitutions"
4060
 
""
4061
 
"A backreference is written @code{\\n} where @code{n} is some single digit"
4062
 
"other than 0.  To be a valid backreference, there must be at least"
4063
 
"@code{n} parenthesized subexpressions in the pattern prior to the"
4064
 
"backreference."
4065
 
""
4066
 
"A backreference matches a literal copy of whatever was matched by the"
4067
 
"corresponding subexpression.  For example,"
4068
 
""
4069
 
"@example"
4070
 
"\\(.*\\)-\\1"
4071
 
"@end example"
4072
 
""
4073
 
"matches:"
4074
 
""
4075
 
"@example"
4076
 
"go-go"
4077
 
"ha-ha"
4078
 
"wakka-wakka"
4079
 
"..."
4080
 
"@end example"
4081
 
""
4082
 
"In some applications, subexpressions are used to extract substrings."
4083
 
"For example, Emacs has the functions @code{match-beginnning} and"
4084
 
"@code{match-end} which report the positions of strings matched by"
4085
 
"subexpressions.  These functions use the same numbering scheme for"
4086
 
"subexpressions as backreferences, with the additional rule that"
4087
 
"subexpression 0 is defined to be the whole regexp."
4088
 
""
4089
 
"In some applications, subexpressions are used in string substitution."
4090
 
"This again uses the backreference numbering scheme. For example, this sed"
4091
 
"command:"
4092
 
""
4093
 
"@example"
4094
 
"s/From:.*<\\(.*\\)>/To: \\1/"
4095
 
"@end example"
4096
 
""
4097
 
"first matches the line:"
4098
 
""
4099
 
"@example"
4100
 
"From: Joe Schmoe <schmoe@@uspringfield.edu>"
4101
 
"@end example"
4102
 
""
4103
 
"when it does, subexpression 1 matches \"schmoe@@uspringfield.edu\"."
4104
 
"The command replaces the matched line with \"To: \\1\" after doing"
4105
 
"subexpression substitution on it to get:"
4106
 
""
4107
 
"@example"
4108
 
"To: schmoe@@uspringfield.edu"
4109
 
"@end example"
4110
 
""
4111
 
""
4112
 
"@node  A Summary of Regexp Syntax, Ambiguous Patterns, Backreferences, Posix Basic Regular Expressions"
4113
 
"@section A Summary of Regexp Syntax"
4114
 
""
4115
 
"In summary, regexps can be:"
4116
 
""
4117
 
"@code{abcd} -- matching a string literally"
4118
 
""
4119
 
"@code{.} -- matching everything except NULL"
4120
 
""
4121
 
"@code{[a-z_?]}, @code{^[a-z_?]}, @code{[[:alpha:]]} and"
4122
 
"@code{[^[:alpha:]]} -- matching character sets"
4123
 
""
4124
 
"@code{\\(subexp\\)} -- grouping an expression into a subexpression."
4125
 
""
4126
 
"@code{\\n} -- match a copy of whatever was matched by the nth subexpression."
4127
 
""
4128
 
"The following special characters and sequences can be applied to a"
4129
 
"character, character set, subexpression, or backreference:"
4130
 
""
4131
 
"@code{*} -- repeat the preceeding element 0 or more times."
4132
 
""
4133
 
"@code{\\+} -- repeat the preceeding element 1 or more times."
4134
 
""
4135
 
"@code{\\?} -- match the preceeding element 0 or 1 time."
4136
 
""
4137
 
"@code{@{m,n@}} -- match the preceeding element at least @code{m}, and as"
4138
 
"many as @code{n} times."
4139
 
""
4140
 
"@code{regexp-1\\|regexp-2\\|..} -- match any regexp-n."
4141
 
""
4142
 
"A special character, like @code{.} or @code{*} can be made into a literal"
4143
 
"character by prefixing it with @code{\\}."
4144
 
""
4145
 
"A special sequence, like @code{\\+} or @code{\\?} can be made into a"
4146
 
"literal character by dropping the @code{\\}."
4147
 
""
4148
 
""
4149
 
"@node  Ambiguous Patterns,  , A Summary of Regexp Syntax, Posix Basic Regular Expressions"
4150
 
"@section Ambiguous Patterns"
4151
 
""
4152
 
"Sometimes a regular expression appears to be ambiguous.  For example, suppose"
4153
 
"we compare the pattern:"
4154
 
""
4155
 
"@example"
4156
 
"begin\\|beginning"
4157
 
"@end example"
4158
 
""
4159
 
"to the string"
4160
 
""
4161
 
"@example"
4162
 
"beginning"
4163
 
"@end example"
4164
 
""
4165
 
"either just the first 5 characters will match, or the whole string will match."
4166
 
""
4167
 
"In every case like this, the longer match is preferred.  The whole"
4168
 
"string will match."
4169
 
""
4170
 
"Sometimes there is ambiguity not about how many characters to match, but"
4171
 
"where the subexpressions occur within the match.  This can effect"
4172
 
"extraction functions like Emacs' @code{match-beginning} or rewrite"
4173
 
"functions like sed's @code{s} command.  For example, consider matching"
4174
 
"the pattern:"
4175
 
""
4176
 
"@example"
4177
 
"b\\(\\[^q]*\\)\\(ing\\)?"
4178
 
"@end example"
4179
 
""
4180
 
"against the string"
4181
 
""
4182
 
"@example"
4183
 
"beginning"
4184
 
"@end example"
4185
 
""
4186
 
"One possibility is that the first subexpression matches \"eginning\" and"
4187
 
"the second is skipped.  Another possibility is that the first"
4188
 
"subexpression matches \"eginn\" and the second matches \"ing\"."
4189
 
""
4190
 
"The rule is that consistant with matching as many characters as "
4191
 
"possible, the length of lower numbered subexpressions is maximized"
4192
 
"in preference to maximizing the length of later subexpressions."
4193
 
""
4194
 
"In the case of the above example, the two possible matches are equal in"
4195
 
"overall length.  Therefore, it comes down to maximizing the"
4196
 
"lower-numbered subexpression, \\1.  The correct answer is that \\1 matches"
4197
 
"\"eginning\" and \\2 is skipped."
4198
 
""
4199
 
"@node Posix Entry Points, Beyond POSIX, Posix Basic Regular Expressions, Top"
4200
 
"@chapter Posix Entry Points"
4201
 
""
4202
 
"This section is excerpted from @emph{The GNU C Library} reference manual"
4203
 
"by Sandra Loosemore with Richard M. Stallman, Roland McGrath, and Andrew"
4204
 
"Oram."
4205
 
""
4206
 
"The GNU C library supports the standard POSIX.2 interface.  Programs"
4207
 
"using this interface should include the header file @file{rxposix.h}."
4208
 
""
4209
 
"@menu"
4210
 
"* POSIX Regexp Compilation::    Using @code{regcomp} to prepare to match."
4211
 
"* Flags for POSIX Regexps::     Syntax variations for @code{regcomp}."
4212
 
"* Matching POSIX Regexps::      Using @code{regexec} to match the compiled"
4213
 
"                                  pattern that you get from @code{regcomp}."
4214
 
"* Regexp Subexpressions::       Finding which parts of the string were matched."
4215
 
"* Subexpression Complications::  Find points of which parts were matched."
4216
 
"* Regexp Cleanup::              Freeing storage; reporting errors."
4217
 
"@end menu"
4218
 
""
4219
 
"@node POSIX Regexp Compilation, Flags for POSIX Regexps, Posix Entry Points, Posix Entry Points"
4220
 
"@section POSIX Regular Expression Compilation"
4221
 
""
4222
 
"Before you can actually match a regular expression, you must"
4223
 
"@dfn{compile} it.  This is not true compilation---it produces a special"
4224
 
"data structure, not machine instructions.  But it is like ordinary"
4225
 
"compilation in that its purpose is to enable you to ``execute'' the"
4226
 
"pattern fast.  (@xref{Matching POSIX Regexps}, for how to use the"
4227
 
"compiled regular expression for matching.)"
4228
 
""
4229
 
"There is a special data type for compiled regular expressions:"
4230
 
""
4231
 
"@comment regex.h"
4232
 
"@comment POSIX.2"
4233
 
"@deftp {Data Type} regex_t"
4234
 
"This type of object holds a compiled regular expression."
4235
 
"It is actually a structure.  It has just one field that your programs"
4236
 
"should look at:"
4237
 
""
4238
 
"@table @code"
4239
 
"@item re_nsub"
4240
 
"This field holds the number of parenthetical subexpressions in the"
4241
 
"regular expression that was compiled."
4242
 
"@end table"
4243
 
""
4244
 
"There are several other fields, but we don't describe them here, because"
4245
 
"only the functions in the library should use them."
4246
 
"@end deftp"
4247
 
""
4248
 
"After you create a @code{regex_t} object, you can compile a regular"
4249
 
"expression into it by calling @code{regcomp}."
4250
 
""
4251
 
"@comment regex.h"
4252
 
"@comment POSIX.2"
4253
 
"@deftypefun int regcomp (regex_t *@var{compiled}, const char *@var{pattern}, int @var{cflags})"
4254
 
"@deftypefunx int regncomp (regex_t *@var{compiled}, int len, const char *@var{pattern}, int @var{cflags})"
4255
 
"The function @code{regcomp} ``compiles'' a regular expression into a"
4256
 
"data structure that you can use with @code{regexec} to match against a"
4257
 
"string.  The compiled regular expression format is designed for"
4258
 
"efficient matching.  @code{regcomp} stores it into @code{*@var{compiled}}."
4259
 
""
4260
 
"The parameter @var{pattern} points to the regular expression to be"
4261
 
"compiled.  When using @code{regcomp}, @var{pattern} must be"
4262
 
"0-terminated.  When using @code{regncomp}, @var{pattern} must be"
4263
 
"@var{len} characters long."
4264
 
""
4265
 
"@code{regncomp} is not a standard function;  strictly POSIX programs"
4266
 
"should avoid using it."
4267
 
""
4268
 
"It's up to you to allocate an object of type @code{regex_t} and pass its"
4269
 
"address to @code{regcomp}."
4270
 
""
4271
 
"The argument @var{cflags} lets you specify various options that control"
4272
 
"the syntax and semantics of regular expressions.  @xref{Flags for POSIX"
4273
 
"Regexps}."
4274
 
""
4275
 
"If you use the flag @code{REG_NOSUB}, then @code{regcomp} omits from"
4276
 
"the compiled regular expression the information necessary to record"
4277
 
"how subexpressions actually match.  In this case, you might as well"
4278
 
"pass @code{0} for the @var{matchptr} and @var{nmatch} arguments when"
4279
 
"you call @code{regexec}."
4280
 
""
4281
 
"If you don't use @code{REG_NOSUB}, then the compiled regular expression"
4282
 
"does have the capacity to record how subexpressions match.  Also,"
4283
 
"@code{regcomp} tells you how many subexpressions @var{pattern} has, by"
4284
 
"storing the number in @code{@var{compiled}->re_nsub}.  You can use that"
4285
 
"value to decide how long an array to allocate to hold information about"
4286
 
"subexpression matches."
4287
 
""
4288
 
"@code{regcomp} returns @code{0} if it succeeds in compiling the regular"
4289
 
"expression; otherwise, it returns a nonzero error code (see the table"
4290
 
"below).  You can use @code{regerror} to produce an error message string"
4291
 
"describing the reason for a nonzero value; see @ref{Regexp Cleanup}."
4292
 
""
4293
 
"@end deftypefun"
4294
 
""
4295
 
"Here are the possible nonzero values that @code{regcomp} can return:"
4296
 
""
4297
 
"@table @code"
4298
 
"@comment regex.h"
4299
 
"@comment POSIX.2"
4300
 
"@item REG_BADBR"
4301
 
"There was an invalid @samp{\\@{@dots{}\\@}} construct in the regular"
4302
 
"expression.  A valid @samp{\\@{@dots{}\\@}} construct must contain either"
4303
 
"a single number, or two numbers in increasing order separated by a"
4304
 
"comma."
4305
 
""
4306
 
"@comment regex.h"
4307
 
"@comment POSIX.2"
4308
 
"@item REG_BADPAT"
4309
 
"There was a syntax error in the regular expression."
4310
 
""
4311
 
"@comment regex.h"
4312
 
"@comment POSIX.2"
4313
 
"@item REG_BADRPT"
4314
 
"A repetition operator such as @samp{?} or @samp{*} appeared in a bad"
4315
 
"position (with no preceding subexpression to act on)."
4316
 
""
4317
 
"@comment regex.h"
4318
 
"@comment POSIX.2"
4319
 
"@item REG_ECOLLATE"
4320
 
"The regular expression referred to an invalid collating element (one not"
4321
 
"defined in the current locale for string collation).  "
4322
 
""
4323
 
"@comment regex.h"
4324
 
"@comment POSIX.2"
4325
 
"@item REG_ECTYPE"
4326
 
"The regular expression referred to an invalid character class name."
4327
 
""
4328
 
"@comment regex.h"
4329
 
"@comment POSIX.2"
4330
 
"@item REG_EESCAPE"
4331
 
"The regular expression ended with @samp{\\}."
4332
 
""
4333
 
"@comment regex.h"
4334
 
"@comment POSIX.2"
4335
 
"@item REG_ESUBREG"
4336
 
"There was an invalid number in the @samp{\\@var{digit}} construct."
4337
 
""
4338
 
"@comment regex.h"
4339
 
"@comment POSIX.2"
4340
 
"@item REG_EBRACK"
4341
 
"There were unbalanced square brackets in the regular expression."
4342
 
""
4343
 
"@comment regex.h"
4344
 
"@comment POSIX.2"
4345
 
"@item REG_EPAREN"
4346
 
"An extended regular expression had unbalanced parentheses,"
4347
 
"or a basic regular expression had unbalanced @samp{\\(} and @samp{\\)}."
4348
 
""
4349
 
"@comment regex.h"
4350
 
"@comment POSIX.2"
4351
 
"@item REG_EBRACE"
4352
 
"The regular expression had unbalanced @samp{\\@{} and @samp{\\@}}."
4353
 
""
4354
 
"@comment regex.h"
4355
 
"@comment POSIX.2"
4356
 
"@item REG_ERANGE"
4357
 
"One of the endpoints in a range expression was invalid."
4358
 
""
4359
 
"@comment regex.h"
4360
 
"@comment POSIX.2"
4361
 
"@item REG_ESPACE"
4362
 
"@code{regcomp} ran out of memory."
4363
 
"@end table"
4364
 
""
4365
 
"@node Flags for POSIX Regexps, Matching POSIX Regexps, POSIX Regexp Compilation, Posix Entry Points"
4366
 
"@section Flags for POSIX Regular Expressions"
4367
 
""
4368
 
"These are the bit flags that you can use in the @var{cflags} operand when"
4369
 
"compiling a regular expression with @code{regcomp}."
4370
 
" "
4371
 
"@table @code"
4372
 
"@comment regex.h"
4373
 
"@comment POSIX.2"
4374
 
"@item REG_EXTENDED"
4375
 
"Treat the pattern as an extended regular expression, rather than as a"
4376
 
"basic regular expression."
4377
 
""
4378
 
"@comment regex.h"
4379
 
"@comment POSIX.2"
4380
 
"@item REG_ICASE"
4381
 
"Ignore case when matching letters."
4382
 
""
4383
 
"@comment regex.h"
4384
 
"@comment POSIX.2"
4385
 
"@item REG_NOSUB"
4386
 
"Don't bother storing the contents of the @var{matches-ptr} array."
4387
 
""
4388
 
"@comment regex.h"
4389
 
"@comment POSIX.2"
4390
 
"@item REG_NEWLINE"
4391
 
"Treat a newline in @var{string} as dividing @var{string} into multiple"
4392
 
"lines, so that @samp{$} can match before the newline and @samp{^} can"
4393
 
"match after.  Also, don't permit @samp{.} to match a newline, and don't"
4394
 
"permit @samp{[^@dots{}]} to match a newline."
4395
 
""
4396
 
"Otherwise, newline acts like any other ordinary character."
4397
 
"@end table"
4398
 
""
4399
 
"@node Matching POSIX Regexps, Regexp Subexpressions, Flags for POSIX Regexps, Posix Entry Points"
4400
 
"@section Matching a Compiled POSIX Regular Expression"
4401
 
""
4402
 
"Once you have compiled a regular expression, as described in @ref{POSIX"
4403
 
"Regexp Compilation}, you can match it against strings using"
4404
 
"@code{regexec}.  A match anywhere inside the string counts as success,"
4405
 
"unless the regular expression contains anchor characters (@samp{^} or"
4406
 
"@samp{$})."
4407
 
""
4408
 
"@comment regex.h"
4409
 
"@comment POSIX.2"
4410
 
"@deftypefun int regexec (regex_t *@var{compiled}, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
4411
 
"@deftypefunx int regnexec (regex_t *@var{compiled}, int len, char *@var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr} @t{[]}, int @var{eflags})"
4412
 
"This function tries to match the compiled regular expression"
4413
 
"@code{*@var{compiled}} against @var{string}."
4414
 
""
4415
 
"@code{regexec} returns @code{0} if the regular expression matches;"
4416
 
"otherwise, it returns a nonzero value.  See the table below for"
4417
 
"what nonzero values mean.  You can use @code{regerror} to produce an"
4418
 
"error message string describing the reason for a nonzero value; "
4419
 
"see @ref{Regexp Cleanup}."
4420
 
""
4421
 
"The parameter @var{string} points to the text to search.  When using"
4422
 
"@code{regexec}, @var{string} must be 0-terminated.  When using"
4423
 
"@code{regnexec}, @var{string} must be @var{len} characters long."
4424
 
""
4425
 
"@code{regnexec} is not a standard function;  strictly POSIX programs"
4426
 
"should avoid using it."
4427
 
""
4428
 
"The argument @var{eflags} is a word of bit flags that enable various"
4429
 
"options."
4430
 
""
4431
 
"If you want to get information about what part of @var{string} actually"
4432
 
"matched the regular expression or its subexpressions, use the arguments"
4433
 
"@var{matchptr} and @var{nmatch}.  Otherwise, pass @code{0} for "
4434
 
"@var{nmatch}, and @code{NULL} for @var{matchptr}.  @xref{Regexp"
4435
 
"Subexpressions}."
4436
 
"@end deftypefun"
4437
 
""
4438
 
"You must match the regular expression with the same set of current"
4439
 
"locales that were in effect when you compiled the regular expression."
4440
 
""
4441
 
"The function @code{regexec} accepts the following flags in the"
4442
 
"@var{eflags} argument:"
4443
 
""
4444
 
"@table @code "
4445
 
"@comment regex.h"
4446
 
"@comment POSIX.2"
4447
 
"@item REG_NOTBOL"
4448
 
"Do not regard the beginning of the specified string as the beginning of"
4449
 
"a line; more generally, don't make any assumptions about what text might"
4450
 
"precede it."
4451
 
""
4452
 
"@comment regex.h"
4453
 
"@comment POSIX.2"
4454
 
"@item REG_NOTEOL"
4455
 
"Do not regard the end of the specified string as the end of a line; more"
4456
 
"generally, don't make any assumptions about what text might follow it."
4457
 
"@end table"
4458
 
""
4459
 
"Here are the possible nonzero values that @code{regexec} can return:"
4460
 
""
4461
 
"@table @code"
4462
 
"@comment regex.h"
4463
 
"@comment POSIX.2"
4464
 
"@item REG_NOMATCH"
4465
 
"The pattern didn't match the string.  This isn't really an error."
4466
 
""
4467
 
"@comment regex.h"
4468
 
"@comment POSIX.2"
4469
 
"@item REG_ESPACE"
4470
 
"@code{regexec} ran out of memory."
4471
 
"@end table"
4472
 
""
4473
 
"@node Regexp Subexpressions, Subexpression Complications, Matching POSIX Regexps, Posix Entry Points"
4474
 
"@section Match Results with Subexpressions"
4475
 
""
4476
 
"When @code{regexec} matches parenthetical subexpressions of"
4477
 
"@var{pattern}, it records which parts of @var{string} they match.  It"
4478
 
"returns that information by storing the offsets into an array whose"
4479
 
"elements are structures of type @code{regmatch_t}.  The first element of"
4480
 
"the array (index @code{0}) records the part of the string that matched"
4481
 
"the entire regular expression.  Each other element of the array records"
4482
 
"the beginning and end of the part that matched a single parenthetical"
4483
 
"subexpression."
4484
 
""
4485
 
"@comment regex.h"
4486
 
"@comment POSIX.2"
4487
 
"@deftp {Data Type} regmatch_t"
4488
 
"This is the data type of the @var{matcharray} array that you pass to"
4489
 
"@code{regexec}.  It containes two structure fields, as follows:"
4490
 
""
4491
 
"@table @code"
4492
 
"@item rm_so"
4493
 
"The offset in @var{string} of the beginning of a substring.  Add this"
4494
 
"value to @var{string} to get the address of that part."
4495
 
""
4496
 
"@item rm_eo"
4497
 
"The offset in @var{string} of the end of the substring."
4498
 
"@end table"
4499
 
"@end deftp"
4500
 
""
4501
 
"@comment regex.h"
4502
 
"@comment POSIX.2"
4503
 
"@deftp {Data Type} regoff_t"
4504
 
"@code{regoff_t} is an alias for another signed integer type."
4505
 
"The fields of @code{regmatch_t} have type @code{regoff_t}."
4506
 
"@end deftp"
4507
 
""
4508
 
"The @code{regmatch_t} elements correspond to subexpressions"
4509
 
"positionally; the first element (index @code{1}) records where the first"
4510
 
"subexpression matched, the second element records the second"
4511
 
"subexpression, and so on.  The order of the subexpressions is the order"
4512
 
"in which they begin."
4513
 
""
4514
 
"When you call @code{regexec}, you specify how long the @var{matchptr}"
4515
 
"array is, with the @var{nmatch} argument.  This tells @code{regexec} how"
4516
 
"many elements to store.  If the actual regular expression has more than"
4517
 
"@var{nmatch} subexpressions, then you won't get offset information about"
4518
 
"the rest of them.  But this doesn't alter whether the pattern matches a"
4519
 
"particular string or not."
4520
 
""
4521
 
"If you don't want @code{regexec} to return any information about where"
4522
 
"the subexpressions matched, you can either supply @code{0} for"
4523
 
"@var{nmatch}, or use the flag @code{REG_NOSUB} when you compile the"
4524
 
"pattern with @code{regcomp}."
4525
 
""
4526
 
"@node Subexpression Complications, Regexp Cleanup, Regexp Subexpressions, Posix Entry Points"
4527
 
"@section Complications in Subexpression Matching"
4528
 
""
4529
 
"Sometimes a subexpression matches a substring of no characters.  This"
4530
 
"happens when @samp{f\\(o*\\)} matches the string @samp{fum}.  (It really"
4531
 
"matches just the @samp{f}.)  In this case, both of the offsets identify"
4532
 
"the point in the string where the null substring was found.  In this"
4533
 
"example, the offsets are both @code{1}."
4534
 
""
4535
 
"Sometimes the entire regular expression can match without using some of"
4536
 
"its subexpressions at all---for example, when @samp{ba\\(na\\)*} matches the"
4537
 
"string @samp{ba}, the parenthetical subexpression is not used.  When"
4538
 
"this happens, @code{regexec} stores @code{-1} in both fields of the"
4539
 
"element for that subexpression."
4540
 
""
4541
 
"Sometimes matching the entire regular expression can match a particular"
4542
 
"subexpression more than once---for example, when @samp{ba\\(na\\)*}"
4543
 
"matches the string @samp{bananana}, the parenthetical subexpression"
4544
 
"matches three times.  When this happens, @code{regexec} usually stores"
4545
 
"the offsets of the last part of the string that matched the"
4546
 
"subexpression.  In the case of @samp{bananana}, these offsets are"
4547
 
"@code{6} and @code{8}."
4548
 
""
4549
 
"But the last match is not always the one that is chosen.  It's more"
4550
 
"accurate to say that the last @emph{opportunity} to match is the one"
4551
 
"that takes precedence.  What this means is that when one subexpression"
4552
 
"appears within another, then the results reported for the inner"
4553
 
"subexpression reflect whatever happened on the last match of the outer"
4554
 
"subexpression.  For an example, consider @samp{\\(ba\\(na\\)*s \\)*} matching"
4555
 
"the string @samp{bananas bas }.  The last time the inner expression"
4556
 
"actually matches is near the end of the first word.  But it is "
4557
 
"@emph{considered} again in the second word, and fails to match there."
4558
 
"@code{regexec} reports nonuse of the ``na'' subexpression."
4559
 
""
4560
 
"Another place where this rule applies is when the regular expression"
4561
 
"@w{@samp{\\(ba\\(na\\)*s \\|nefer\\(ti\\)* \\)*}} matches @samp{bananas nefertiti}."
4562
 
"The ``na'' subexpression does match in the first word, but it doesn't"
4563
 
"match in the second word because the other alternative is used there."
4564
 
"Once again, the second repetition of the outer subexpression overrides"
4565
 
"the first, and within that second repetition, the ``na'' subexpression"
4566
 
"is not used.  So @code{regexec} reports nonuse of the ``na''"
4567
 
"subexpression."
4568
 
""
4569
 
"@node Regexp Cleanup,  , Subexpression Complications, Posix Entry Points"
4570
 
"@section POSIX Regexp Matching Cleanup"
4571
 
""
4572
 
"When you are finished using a compiled regular expression, you can"
4573
 
"free the storage it uses by calling @code{regfree}."
4574
 
""
4575
 
"@comment regex.h"
4576
 
"@comment POSIX.2"
4577
 
"@deftypefun void regfree (regex_t *@var{compiled})"
4578
 
"Calling @code{regfree} frees all the storage that @code{*@var{compiled}}"
4579
 
"points to.  This includes various internal fields of the @code{regex_t}"
4580
 
"structure that aren't documented in this manual."
4581
 
""
4582
 
"@code{regfree} does not free the object @code{*@var{compiled}} itself."
4583
 
"@end deftypefun"
4584
 
""
4585
 
"You should always free the space in a @code{regex_t} structure with"
4586
 
"@code{regfree} before using the structure to compile another regular"
4587
 
"expression."
4588
 
""
4589
 
"When @code{regcomp} or @code{regexec} reports an error, you can use"
4590
 
"the function @code{regerror} to turn it into an error message string."
4591
 
""
4592
 
"@comment regex.h"
4593
 
"@comment POSIX.2"
4594
 
"@deftypefun size_t regerror (int @var{errcode}, regex_t *@var{compiled}, char *@var{buffer}, size_t @var{length})"
4595
 
"This function produces an error message string for the error code"
4596
 
"@var{errcode}, and stores the string in @var{length} bytes of memory"
4597
 
"starting at @var{buffer}.  For the @var{compiled} argument, supply the"
4598
 
"same compiled regular expression structure that @code{regcomp} or"
4599
 
"@code{regexec} was working with when it got the error.  Alternatively,"
4600
 
"you can supply @code{NULL} for @var{compiled}; you will still get a"
4601
 
"meaningful error message, but it might not be as detailed."
4602
 
""
4603
 
"If the error message can't fit in @var{length} bytes (including a"
4604
 
"terminating null character), then @code{regerror} truncates it."
4605
 
"The string that @code{regerror} stores is always null-terminated"
4606
 
"even if it has been truncated."
4607
 
""
4608
 
"The return value of @code{regerror} is the minimum length needed to"
4609
 
"store the entire error message.  If this is less than @var{length}, then"
4610
 
"the error message was not truncated, and you can use it.  Otherwise, you"
4611
 
"should call @code{regerror} again with a larger buffer."
4612
 
""
4613
 
"Here is a function which uses @code{regerror}, but always dynamically"
4614
 
"allocates a buffer for the error message:"
4615
 
""
4616
 
"@smallexample"
4617
 
"char *get_regerror (int errcode, regex_t *compiled)"
4618
 
"@{"
4619
 
"  size_t length = regerror (errcode, compiled, NULL, 0);"
4620
 
"  char *buffer = xmalloc (length);"
4621
 
"  (void) regerror (errcode, compiled, buffer, length);"
4622
 
"  return buffer;"
4623
 
"@}"
4624
 
"@end smallexample"
4625
 
"@end deftypefun"
4626
 
""
4627
 
"@node  Beyond POSIX, Rx Theory, Posix Entry Points, Top"
4628
 
"@chapter Beyond POSIX"
4629
 
""
4630
 
"This section is not finished documentation, but rather a collection of"
4631
 
"pointers towards some of the interesting, non-standard features of Rx."
4632
 
""
4633
 
"@section New Regexp Operators"
4634
 
""
4635
 
"Rx supports some unusual regexp syntax."
4636
 
""
4637
 
"@code{[[:cut N:]]} sets @code{pmatch[0].final_tag} to N and causes the"
4638
 
"matching to stop instantly.  If N is 0, the overall match fails,"
4639
 
"otherwise it succeeds."
4640
 
""
4641
 
"@code{[[:(:]] ... [[:):]]} is just like @code{\\( ... \\)} except that in"
4642
 
"the first case, no pmatch entries are changed, and the subexpression is"
4643
 
"not counted in the numbering of parenthesized subexpressions."
4644
 
""
4645
 
"@code{[[:(:]] ... [[:):]]} can be used when you do not need to know"
4646
 
"where a subexpression matched but are only using parentheses to effect"
4647
 
"the parsing of the regexp.  "
4648
 
""
4649
 
"There are two reasons to use @code{[[:(:]] ... [[:):]]}:"
4650
 
""
4651
 
"1. regexec will run faster."
4652
 
""
4653
 
"2. Currently, only 8 backreferencable subexpressions are supported:"
4654
 
"@code{\\1 .. \\9}.  Using @code{[[:(:]] ... [[:):]]} is a way to conserve"
4655
 
"backreferencable subexpression names in an expression with many"
4656
 
"parentheses."
4657
 
""
4658
 
"@section New POSIX Functions"
4659
 
""
4660
 
"@code{regncomp} and @code{regnexec} are non-standard generalizations of"
4661
 
"@code{regcomp} and @code{regexec}."
4662
 
""
4663
 
"@section Tuning POSIX performance"
4664
 
""
4665
 
"Two mysterious parmaters can be used to trade-off performance and"
4666
 
"memory use."
4667
 
""
4668
 
"At compile-time they are @code{RX_DEFAULT_DFA_CACHE_SIZE} and"
4669
 
"@code{RX_DEFAULT_NFA_DELAY}.  "
4670
 
""
4671
 
"If you want to mess with these (I generally don't advise it), I suggest"
4672
 
"experimenting for your particular application/memory situation; frob"
4673
 
"these by powers of two and try out the results on what you expect will"
4674
 
"be typical regexp workloads."
4675
 
""
4676
 
"You can also set those parameters at run-time (before calling any regexp"
4677
 
"functions) by tweaking the corresponding variables:"
4678
 
""
4679
 
"@code{rx_default_cache->bytes_allowed}"
4680
 
""
4681
 
"and"
4682
 
""
4683
 
"@code{rx_basic_unfaniverse_delay}"
4684
 
""
4685
 
""
4686
 
""
4687
 
"@section POSIX stream-style interface"
4688
 
""
4689
 
"@code{rx_make_solutions}, @code{rx_next_solution}, and"
4690
 
"@code{rx_free_solutions} are a lower level alternative to the posix"
4691
 
"functions.  Using those functions, you can compare a compiled regexp to"
4692
 
"a string that is not contiguous in memory or even a string that is not"
4693
 
"entirely in memory at any one time."
4694
 
""
4695
 
"The code in rxposix.c points out how those functions are used."
4696
 
""
4697
 
""
4698
 
"@section DFAs Directly"
4699
 
""
4700
 
"If you are only interested in pure regular expressions (no pmatch data,"
4701
 
"no backreferences, and no counted subexpressions), you can parse a"
4702
 
"regexp using @code{rx_parse}, convert it to an nfa using @code{rx_unfa},"
4703
 
"and run the dfa using @code{rx_init_system}, @code{rx_advance_to_final},"
4704
 
"and @code{rx_terminate_system}.  The dfa Scheme primitives in"
4705
 
"@file{rgx.c} may provide some guide."
4706
 
""
4707
 
"@node    Rx Theory,  , Beyond POSIX, Top"
4708
 
"@chapter Rx Theory"
4709
 
"@cindex Rx Theory"
4710
 
""
4711
 
"There are two match algorithms.  One is for truly regular regexps (those"
4712
 
"that can be reduced to a dfa).  The other is for non-regular regexps."
4713
 
""
4714
 
"The dfa algorithm implements the idea suggested in @cite{Compilers} by"
4715
 
"Aho, Sethi and Ullman:"
4716
 
""
4717
 
"@quotation"
4718
 
"[One] approach [to pattern matching regular expressions] is to use a"
4719
 
"DFA, but avoid constructing all of the transition table by using a"
4720
 
"technique called \"lazy transition evaluation\".  Here, transitions are"
4721
 
"computed at run time [when] actually needed. [T]ransitions are"
4722
 
"stored in a cache. [....] If the cache becomes full, we can erase some"
4723
 
"previously computed transition to make room for the new transition."
4724
 
"@end quotation"
4725
 
""
4726
 
"The implementation in Rx is generalized from that, but the above"
4727
 
"description covers what is used for Posix patterns. "
4728
 
""
4729
 
"The non-dfa algorithm implements a \"recursive decomposition\" technique"
4730
 
"described in email by Henry Spencer.  For a given pattern, this"
4731
 
"algorithm first checks to see if a simpler, superset language,"
4732
 
"DFA-pattern matches.  If it does, then this algorithm does the"
4733
 
"detail-work to see if the non-DFA pattern matches."
4734
 
""
4735
 
"The detail work usually involves recursing on subpatterns.  For example,"
4736
 
"a concatentation of two subexpressions matches a string if the string"
4737
 
"can be divided into two parts, each matching one subexpression, in the"
4738
 
"right order.  More than one solution is often possible for a given"
4739
 
"pattern.  This ambiguity is the subject of the \"leftmost longest\" rules"
4740
 
"in the spec, and the back-tracking oriented stream-of-solution functions"
4741
 
"@code{rx_make_solutions}, @code{rx_next_solution} and"
4742
 
"@code{rx_free_solutions}."
4743
 
""
4744
 
"@example"
4745
 
"rxspencer.[ch]                         -- The non-DFA algorithm"
4746
 
"rxanal.[ch] rxsuper.[ch] rxnfa.[ch]    -- The DFA algorithm"
4747
 
"@end example"
4748
 
""
4749
 
"@bye"
4750
 
""
4751
 
"";