Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[04478c]: regexp.html Maximize Restore History

Download this file

regexp.html    777 lines (607 with data), 27.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
<HTML>
<HEAD>
<!-- Created by texi2html 1.56k from regexp.texinfo on 12 September 2001 -->
<TITLE>Untitled Document</TITLE>
</HEAD>
<BODY>
<H1>Untitled Document</H1>
<P>
<P><HR><P>
<H1>Table of Contents</H1>
<UL>
<LI><A NAME="TOC1" HREF="regexp.html#SEC1">Regular expressions</A>
<LI><A NAME="TOC2" HREF="regexp.html#SEC2">Regular Expressions</A>
<UL>
<LI><A NAME="TOC3" HREF="regexp.html#SEC3">Syntax of Regular Expressions</A>
</UL>
<LI><A NAME="TOC4" HREF="regexp.html#SEC4">Examples</A>
<UL>
<LI><A NAME="TOC5" HREF="regexp.html#SEC5">Complex Regexp Example</A>
<LI><A NAME="TOC6" HREF="regexp.html#SEC6">Common Regular Expressions Used in Editing</A>
</UL>
<LI><A NAME="TOC7" HREF="regexp.html#SEC7">The Regular Expression Module</A>
</UL>
<P><HR><P>
<P>
@newindex {fn}
<H1><A NAME="SEC1" HREF="regexp.html#TOC1">Regular expressions</A></H1>
<P>
Regular expressions are patterns used in selecting text.
<P>
In addition to a specifying string literals, regular expressions can
represent classes of strings. Strings thus represented are said to be
matched by the corresponding regular expression. If it is possible for
a regular expression to match several strings in a line, then the
left-most longest match is the one selected.
<P>
The following symbols are used in constructing regular expressions:
<DL COMPACT>
<DT><CODE><VAR>c</VAR></CODE>
<DD>
Any character <VAR>c</VAR> not listed below, including <SAMP>`{'</SAMP>, <SAMP>`}'</SAMP>,
<SAMP>`('</SAMP>, <SAMP>`)'</SAMP>, <SAMP>`&#60;'</SAMP> and <SAMP>`&#62;'</SAMP>, matches itself.
<DT><CODE>\<VAR>c</VAR></CODE>
<DD>
Any backslash-escaped character <VAR>c</VAR>, other than <SAMP>`{'</SAMP>,
`<SAMP>`}'</SAMP>, <SAMP>`('</SAMP>, <SAMP>`)'</SAMP>, <SAMP>`&#60;'</SAMP>, <SAMP>`&#62;'</SAMP>, <SAMP>`b'</SAMP>, <SAMP>`B'</SAMP>,
<SAMP>`w'</SAMP>, <SAMP>`W'</SAMP>, <SAMP>`+'</SAMP> and <SAMP>`?'</SAMP>, matches itself.
Note that <SAMP>`\'</SAMP> also has special meaning in the read syntax of Lisp
strings, and must be quoted with <SAMP>`\'</SAMP>. For
example, the regular expression that matches the <SAMP>`\'</SAMP> character is
<SAMP>`\\'</SAMP>. To write a Lisp string that contains the characters
<SAMP>`\\'</SAMP>, Lisp syntax requires you to quote each <SAMP>`\'</SAMP> with another
<SAMP>`\'</SAMP>. Therefore, the read syntax for a regular expression matching
<SAMP>`\'</SAMP> is <CODE>"\\\\"</CODE>.
<DT><CODE>.</CODE>
<DD>
Matches any single character.
<DT><CODE>[<VAR>char-class</VAR>]</CODE>
<DD>
Matches any single character in <VAR>char-class</VAR>. To include a <SAMP>`]'</SAMP>
in <VAR>char-class</VAR>, it must be the first character. A range of
characters may be specified by separating the end characters of the
range with a <SAMP>`-'</SAMP>, e.g., <SAMP>`a-z'</SAMP> specifies the lower case
characters. The following literal expressions can also be used in
<VAR>char-class</VAR> to specify sets of characters:
<PRE>
[:alnum:] [:cntrl:] [:lower:] [:space:]
[:alpha:] [:digit:] [:print:] [:upper:]
[:blank:] [:graph:] [:punct:] [:xdigit:]
</PRE>
If <SAMP>`-'</SAMP> appears as the first or last character of <VAR>char-class</VAR>,
then it matches itself. All other characters in <VAR>char-class</VAR> match
themselves.
Patterns in
<VAR>char-class</VAR>
of the form:
<PRE>
[.<VAR>col-elm</VAR>.]
[=<VAR>col-elm</VAR>=]
</PRE>
where <VAR>col-elm</VAR> is a <EM>collating element</EM> are interpreted
according to <CODE>locale (5)</CODE> (not currently supported). See
<CODE>regex (3)</CODE> for an explanation of these constructs.
<DT><CODE>[^<VAR>char-class</VAR>]</CODE>
<DD>
Matches any single character, other than newline, not in
<VAR>char-class</VAR>. <VAR>char-class</VAR> is defined as above.
<DT><CODE>^</CODE>
<DD>
If <SAMP>`^'</SAMP> is the first character of a regular expression, then it
anchors the regular expression to the beginning of a line. Otherwise,
it matches itself.
<DT><CODE>$</CODE>
<DD>
If <SAMP>`$'</SAMP> is the last character of a regular expression, it anchors
the regular expression to the end of a line. Otherwise, it matches
itself.
<DT><CODE>\(<VAR>re</VAR>\)</CODE>
<DD>
Defines a (possibly null) subexpression <VAR>re</VAR>.
Subexpressions may be nested. A
subsequent backreference of the form <SAMP>`\<VAR>n</VAR>'</SAMP>, where <VAR>n</VAR> is a
number in the range [1,9], expands to the text matched by the <VAR>n</VAR>th
subexpression. For example, the regular expression <SAMP>`\(a.c\)\1'</SAMP> matches
the string <SAMP>`abcabc'</SAMP>, but not <SAMP>`abcadc'</SAMP>.
Subexpressions are ordered relative to their left delimiter.
<DT><CODE>*</CODE>
<DD>
Matches the single character regular expression or subexpression
immediately preceding it zero or more times. If <SAMP>`*'</SAMP> is the first
character of a regular expression or subexpression, then it matches
itself. The <SAMP>`*'</SAMP> operator sometimes yields unexpected results. For
example, the regular expression <SAMP>`b*'</SAMP> matches the beginning of the
string <SAMP>`abbb'</SAMP>, as opposed to the substring <SAMP>`bbb'</SAMP>, since a
null match is the only left-most match.
<DT><CODE>\{<VAR>n,m</VAR>\}</CODE>
<DD>
<DT><CODE>\{<VAR>n,</VAR>\}</CODE>
<DD>
<DT><CODE>\{<VAR>n</VAR>\}</CODE>
<DD>
Matches the single character regular expression or subexpression
immediately preceding it at least <VAR>n</VAR> and at most <VAR>m</VAR> times. If
<VAR>m</VAR> is omitted, then it matches at least <VAR>n</VAR> times. If the
comma is also omitted, then it matches exactly <VAR>n</VAR> times.
If any of these forms occurs first in a regular expression or subexpression,
then it is interpreted literally (i.e., the regular expression <SAMP>`\{2\}'</SAMP>
matches the string <SAMP>`{2}'</SAMP>, and so on).
<DT><CODE>\&#60;</CODE>
<DD>
<DT><CODE>\&#62;</CODE>
<DD>
Anchors the single character regular expression or subexpression
immediately following it to the beginning (in the case of <SAMP>`\&#60;'</SAMP>)
or ending (in the case of <SAMP>`\&#62;'</SAMP>) of
a <EM>word</EM>, i.e., in ASCII, a maximal string of alphanumeric characters,
including the underscore (_).
</DL>
<P>
The following extended operators are preceded by a backslash <SAMP>`\'</SAMP> to
distinguish them from traditional <CODE>ed</CODE> syntax.
<DL COMPACT>
<DT><CODE>\`</CODE>
<DD>
<DT><CODE>\'</CODE>
<DD>
Unconditionally matches the beginning <SAMP>`\`'</SAMP> or ending <SAMP>`\''</SAMP> of a line.
<DT><CODE>\?</CODE>
<DD>
Optionally matches the single character regular expression or subexpression
immediately preceding it. For example, the regular expression <SAMP>`a[bd]\?c'</SAMP>
matches the strings <SAMP>`abc'</SAMP>, <SAMP>`adc'</SAMP> and <SAMP>`ac'</SAMP>.
If <SAMP>`\?'</SAMP> occurs at the beginning
of a regular expressions or subexpression, then it matches a literal <SAMP>`?'</SAMP>.
<DT><CODE>\+</CODE>
<DD>
Matches the single character regular expression or subexpression
immediately preceding it one or more times. So the regular expression
<SAMP>`a+'</SAMP> is shorthand for <SAMP>`aa*'</SAMP>. If <SAMP>`\+'</SAMP> occurs at the
beginning of a regular expression or subexpression, then it matches a
literal <SAMP>`+'</SAMP>.
<DT><CODE>\b</CODE>
<DD>
Matches the beginning or ending (null string) of a word. Thus the regular
expression <SAMP>`\bhello\b'</SAMP> is equivalent to <SAMP>`\&#60;hello\&#62;'</SAMP>.
However, <SAMP>`\b\b'</SAMP>
is a valid regular expression whereas <SAMP>`\&#60;\&#62;'</SAMP> is not.
<DT><CODE>\B</CODE>
<DD>
Matches (a null string) inside a word.
<DT><CODE>\w</CODE>
<DD>
Matches any character in a word.
<DT><CODE>\W</CODE>
<DD>
Matches any character not in a word.
</DL>
<H1><A NAME="SEC2" HREF="regexp.html#TOC2">Regular Expressions</A></H1>
<P>
<A NAME="IDX1"></A>
<A NAME="IDX2"></A>
<P>
A <EM>regular expression</EM> (<EM>regexp</EM>, for short) is a pattern that
denotes a (possibly infinite) set of strings. Searching for matches for
a regexp is a very powerful operation. This section explains how to write
regexps; the following section says how to search for them.
<H2><A NAME="SEC3" HREF="regexp.html#TOC3">Syntax of Regular Expressions</A></H2>
<P>
Regular expressions have a syntax in which a few characters are special
constructs and the rest are <EM>ordinary</EM>. An ordinary character is a
simple regular expression which matches that character and nothing else.
The special characters are <SAMP>`$'</SAMP>, <SAMP>`^'</SAMP>, <SAMP>`.'</SAMP>, <SAMP>`*'</SAMP>,
<SAMP>`['</SAMP>, <SAMP>`]'</SAMP> and <SAMP>`\'</SAMP>; no new special
characters will be defined in the future. Any other character appearing
in a regular expression is ordinary, unless a <SAMP>`\'</SAMP> precedes it.
<P>
For example, <SAMP>`f'</SAMP> is not a special character, so it is ordinary, and
therefore <SAMP>`f'</SAMP> is a regular expression that matches the string
<SAMP>`f'</SAMP> and no other string. (It does <EM>not</EM> match the string
<SAMP>`ff'</SAMP>.) Likewise, <SAMP>`o'</SAMP> is a regular expression that matches
only <SAMP>`o'</SAMP>.
<P>
Any two regular expressions <VAR>a</VAR> and <VAR>b</VAR> can be concatenated. The
result is a regular expression which matches a string if <VAR>a</VAR> matches
some amount of the beginning of that string and <VAR>b</VAR> matches the rest of
the string.
<P>
As a simple example, we can concatenate the regular expressions <SAMP>`f'</SAMP>
and <SAMP>`o'</SAMP> to get the regular expression <SAMP>`fo'</SAMP>, which matches only
the string <SAMP>`fo'</SAMP>. Still trivial. To do something more powerful, you
need to use one of the special characters. Here is a list of them:
<DL COMPACT>
<DT><KBD>. (Period)</KBD>
<DD>
<A NAME="IDX3"></A>
is a special character that matches any single character.
Using concatenation, we can make regular expressions like <SAMP>`a.b'</SAMP>, which
matches any three-character string that begins with <SAMP>`a'</SAMP> and ends with
<SAMP>`b'</SAMP>.
<DT><KBD>*</KBD>
<DD>
<A NAME="IDX4"></A>
is not a construct by itself; it is a suffix operator that means to
repeat the preceding regular expression as many times as possible. In
<SAMP>`fo*'</SAMP>, the <SAMP>`*'</SAMP> applies to the <SAMP>`o'</SAMP>, so <SAMP>`fo*'</SAMP> matches
one <SAMP>`f'</SAMP> followed by any number of <SAMP>`o'</SAMP>s. The case of zero
<SAMP>`o'</SAMP>s is allowed: <SAMP>`fo*'</SAMP> does match <SAMP>`f'</SAMP>.
<SAMP>`*'</SAMP> always applies to the <EM>smallest</EM> possible preceding
expression. Thus, <SAMP>`fo*'</SAMP> has a repeating <SAMP>`o'</SAMP>, not a
repeating <SAMP>`fo'</SAMP>.
The matcher processes a <SAMP>`*'</SAMP> construct by matching, immediately,
as many repetitions as can be found. Then it continues with the rest
of the pattern. If that fails, backtracking occurs, discarding some
of the matches of the <SAMP>`*'</SAMP>-modified construct in case that makes
it possible to match the rest of the pattern. For example, in matching
<SAMP>`ca*ar'</SAMP> against the string <SAMP>`caaar'</SAMP>, the <SAMP>`a*'</SAMP> first
tries to match all three <SAMP>`a'</SAMP>s; but the rest of the pattern is
<SAMP>`ar'</SAMP> and there is only <SAMP>`r'</SAMP> left to match, so this try fails.
The next alternative is for <SAMP>`a*'</SAMP> to match only two <SAMP>`a'</SAMP>s.
With this choice, the rest of the regexp matches successfully.
<DT><KBD>[ ... ]</KBD>
<DD>
<A NAME="IDX5"></A>
<A NAME="IDX6"></A>
<A NAME="IDX7"></A>
<SAMP>`['</SAMP> begins a <EM>character set</EM>, which is terminated by a
<SAMP>`]'</SAMP>. In the simplest case, the characters between the two brackets
form the set. Thus, <SAMP>`[ad]'</SAMP> matches either one <SAMP>`a'</SAMP> or one
<SAMP>`d'</SAMP>, and <SAMP>`[ad]*'</SAMP> matches any string composed of just <SAMP>`a'</SAMP>s
and <SAMP>`d'</SAMP>s (including the empty string), from which it follows that
<SAMP>`c[ad]*r'</SAMP> matches <SAMP>`cr'</SAMP>, <SAMP>`car'</SAMP>, <SAMP>`cdr'</SAMP>,
<SAMP>`caddaar'</SAMP>, etc.
The usual regular expression special characters are not special inside a
character set. A completely different set of special characters exists
inside character sets: <SAMP>`]'</SAMP>, <SAMP>`-'</SAMP> and <SAMP>`^'</SAMP>.
<SAMP>`-'</SAMP> is used for ranges of characters. To write a range, write two
characters with a <SAMP>`-'</SAMP> between them. Thus, <SAMP>`[a-z]'</SAMP> matches any
lower case letter. Ranges may be intermixed freely with individual
characters, as in <SAMP>`[a-z$%.]'</SAMP>, which matches any lower case letter
or <SAMP>`$'</SAMP>, <SAMP>`%'</SAMP> or a period.
The following literal expressions can also be used in
<VAR>char-class</VAR> to specify sets of characters:
<PRE>
[:alnum:] [:cntrl:] [:lower:] [:space:]
[:alpha:] [:digit:] [:print:] [:upper:]
[:blank:] [:graph:] [:punct:] [:xdigit:]
</PRE>
To include a <SAMP>`]'</SAMP> in a character set, make it the first character.
For example, <SAMP>`[]a]'</SAMP> matches <SAMP>`]'</SAMP> or <SAMP>`a'</SAMP>. To include a
<SAMP>`-'</SAMP>, write <SAMP>`-'</SAMP> as the first character in the set, or put
immediately after a range. (You can replace one individual character
<VAR>c</VAR> with the range <SAMP>`<VAR>c</VAR>-<VAR>c</VAR>'</SAMP> to make a place to put the
<SAMP>`-'</SAMP>). There is no way to write a set containing just <SAMP>`-'</SAMP> and
<SAMP>`]'</SAMP>.
To include <SAMP>`^'</SAMP> in a set, put it anywhere but at the beginning of
the set.
<DT><KBD>[^ ... ]</KBD>
<DD>
<A NAME="IDX8"></A>
<SAMP>`[^'</SAMP> begins a <EM>complement character set</EM>, which matches any
character except the ones specified. Thus, <SAMP>`[^a-z0-9A-Z]'</SAMP>
matches all characters <EM>except</EM> letters and digits.
<SAMP>`^'</SAMP> is not special in a character set unless it is the first
character. The character following the <SAMP>`^'</SAMP> is treated as if it
were first (thus, <SAMP>`-'</SAMP> and <SAMP>`]'</SAMP> are not special there).
Note that a complement character set can match a newline, unless
newline is mentioned as one of the characters not to match.
<DT><KBD>^</KBD>
<DD>
<A NAME="IDX9"></A>
<A NAME="IDX10"></A>
is a special character that matches the empty string, but only at
the beginning of a line in the text being matched. Otherwise it fails
to match anything. Thus, <SAMP>`^foo'</SAMP> matches a <SAMP>`foo'</SAMP> which occurs
at the beginning of a line.
When matching a string, <SAMP>`^'</SAMP> matches at the beginning of the string
or after a newline character <SAMP>`\n'</SAMP>.
<DT><KBD>$</KBD>
<DD>
<A NAME="IDX11"></A>
is similar to <SAMP>`^'</SAMP> but matches only at the end of a line. Thus,
<SAMP>`x+$'</SAMP> matches a string of one <SAMP>`x'</SAMP> or more at the end of a line.
When matching a string, <SAMP>`$'</SAMP> matches at the end of the string
or before a newline character <SAMP>`\n'</SAMP>.
<DT><KBD>\</KBD>
<DD>
<A NAME="IDX12"></A>
has two functions: it quotes the special characters (including
<SAMP>`\'</SAMP>), and it introduces additional special constructs.
Because <SAMP>`\'</SAMP> quotes special characters, <SAMP>`\$'</SAMP> is a regular
expression which matches only <SAMP>`$'</SAMP>, and <SAMP>`\['</SAMP> is a regular
expression which matches only <SAMP>`['</SAMP>, and so on.
Note that <SAMP>`\'</SAMP> also has special meaning in the read syntax of Lisp
strings, and must be quoted with <SAMP>`\'</SAMP>. For
example, the regular expression that matches the <SAMP>`\'</SAMP> character is
<SAMP>`\\'</SAMP>. To write a Lisp string that contains the characters
<SAMP>`\\'</SAMP>, Lisp syntax requires you to quote each <SAMP>`\'</SAMP> with another
<SAMP>`\'</SAMP>. Therefore, the read syntax for a regular expression matching
<SAMP>`\'</SAMP> is <CODE>"\\\\"</CODE>.</DL>
<P>
For the most part, <SAMP>`\'</SAMP> followed by any character matches only
that character. However, there are several exceptions: characters
which, when preceded by <SAMP>`\'</SAMP>, are special constructs. Such
characters are always ordinary when encountered on their own. Here
is a table of <SAMP>`\'</SAMP> constructs:
<DL COMPACT>
<DT><KBD>\+</KBD>
<DD>
<A NAME="IDX13"></A>
is a suffix operator similar to <SAMP>`*'</SAMP> except that the preceding
expression must match at least once. So, for example, <SAMP>`ca+r'</SAMP>
matches the strings <SAMP>`car'</SAMP> and <SAMP>`caaaar'</SAMP> but not the string
<SAMP>`cr'</SAMP>, whereas <SAMP>`ca*r'</SAMP> matches all three strings.
<DT><KBD>\?</KBD>
<DD>
<A NAME="IDX14"></A>
is a suffix operator similar to <SAMP>`*'</SAMP> except that the preceding
expression can match either once or not at all. For example,
<SAMP>`ca?r'</SAMP> matches <SAMP>`car'</SAMP> or <SAMP>`cr'</SAMP>, but does not match anyhing
else.
<DT><KBD>\|</KBD>
<DD>
<A NAME="IDX15"></A>
<A NAME="IDX16"></A>
specifies an alternative.
Two regular expressions <VAR>a</VAR> and <VAR>b</VAR> with <SAMP>`\|'</SAMP> in
between form an expression that matches anything that either <VAR>a</VAR> or
<VAR>b</VAR> matches.
Thus, <SAMP>`foo\|bar'</SAMP> matches either <SAMP>`foo'</SAMP> or <SAMP>`bar'</SAMP>
but no other string.
<SAMP>`\|'</SAMP> applies to the largest possible surrounding expressions. Only a
surrounding <SAMP>`\( ... \)'</SAMP> grouping can limit the grouping power of
<SAMP>`\|'</SAMP>.
Full backtracking capability exists to handle multiple uses of <SAMP>`\|'</SAMP>.
<DT><KBD>\( ... \)</KBD>
<DD>
<A NAME="IDX17"></A>
<A NAME="IDX18"></A>
<A NAME="IDX19"></A>
is a grouping construct that serves three purposes:
<OL>
<LI>
To enclose a set of <SAMP>`\|'</SAMP> alternatives for other operations.
Thus, <SAMP>`\(foo\|bar\)x'</SAMP> matches either <SAMP>`foox'</SAMP> or <SAMP>`barx'</SAMP>.
<LI>
To enclose an expression for a suffix operator such as <SAMP>`*'</SAMP> to act
on. Thus, <SAMP>`ba\(na\)*'</SAMP> matches <SAMP>`bananana'</SAMP>, etc., with any
(zero or more) number of <SAMP>`na'</SAMP> strings.
<LI>
To record a matched substring for future reference.
</OL>
This last application is not a consequence of the idea of a
parenthetical grouping; it is a separate feature which happens to be
assigned as a second meaning to the same <SAMP>`\( ... \)'</SAMP> construct
because there is no conflict in practice between the two meanings.
Here is an explanation of this feature:
<DT><KBD>\<VAR>digit</VAR></KBD>
<DD>
matches the same text which matched the <VAR>digit</VAR>th occurrence of a
<SAMP>`\( ... \)'</SAMP> construct.
In other words, after the end of a <SAMP>`\( ... \)'</SAMP> construct. the
matcher remembers the beginning and end of the text matched by that
construct. Then, later on in the regular expression, you can use
<SAMP>`\'</SAMP> followed by <VAR>digit</VAR> to match that same text, whatever it
may have been.
The strings matching the first nine <SAMP>`\( ... \)'</SAMP> constructs
appearing in a regular expression are assigned numbers 1 through 9 in
the order that the open parentheses appear in the regular expression.
So you can use <SAMP>`\1'</SAMP> through <SAMP>`\9'</SAMP> to refer to the text matched
by the corresponding <SAMP>`\( ... \)'</SAMP> constructs.
For example, <SAMP>`\(.*\)\1'</SAMP> matches any newline-free string that is
composed of two identical halves. The <SAMP>`\(.*\)'</SAMP> matches the first
half, which may be anything, but the <SAMP>`\1'</SAMP> that follows must match
the same exact text.
<DT><KBD>\w</KBD>
<DD>
<A NAME="IDX20"></A>
matches any word-constituent character.
<DT><KBD>\W</KBD>
<DD>
<A NAME="IDX21"></A>
matches any character that is not a word-constituent.
</DL>
<P>
These regular expression constructs match the empty string--that is,
they don't use up any characters--but whether they match depends on the
context.
<DL COMPACT>
<DT><KBD>\`</KBD>
<DD>
<A NAME="IDX22"></A>
matches the empty string, but only at the beginning
of the buffer or string being matched against.
<DT><KBD>\'</KBD>
<DD>
<A NAME="IDX23"></A>
matches the empty string, but only at the end of
the buffer or string being matched against.
<DT><KBD>\b</KBD>
<DD>
<A NAME="IDX24"></A>
matches the empty string, but only at the beginning or
end of a word. Thus, <SAMP>`\bfoo\b'</SAMP> matches any occurrence of
<SAMP>`foo'</SAMP> as a separate word. <SAMP>`\bballs?\b'</SAMP> matches
<SAMP>`ball'</SAMP> or <SAMP>`balls'</SAMP> as a separate word.
<DT><KBD>\B</KBD>
<DD>
<A NAME="IDX25"></A>
matches the empty string, but <EM>not</EM> at the beginning or
end of a word.
<DT><KBD>\&#60;</KBD>
<DD>
<A NAME="IDX26"></A>
matches the empty string, but only at the beginning of a word.
<DT><KBD>\&#62;</KBD>
<DD>
<A NAME="IDX27"></A>
matches the empty string, but only at the end of a word.
</DL>
<P>
<A NAME="IDX28"></A>
Not every string is a valid regular expression. For example, a string
with unbalanced square brackets is invalid (with a few exceptions, such
as <SAMP>`[]]'</SAMP>, and so is a string that ends with a single <SAMP>`\'</SAMP>. If
an invalid regular expression is passed to any of the search functions,
an <CODE>invalid-regexp</CODE> error is signaled.
<H1><A NAME="SEC4" HREF="regexp.html#TOC4">Examples</A></H1>
<H2><A NAME="SEC5" HREF="regexp.html#TOC5">Complex Regexp Example</A></H2>
<P>
Here is a complicated regexp, used by Emacs to recognize the end of a
sentence together with any whitespace that follows. It is the value of
the variable <CODE>sentence-end</CODE>.
<P>
First, we show the regexp as a string in C syntax to distinguish
spaces from tab characters. The string constant begins and ends with a
double-quote. <SAMP>`\"'</SAMP> stands for a double-quote as part of the
string, <SAMP>`\\'</SAMP> for a backslash as part of the string, <SAMP>`\t'</SAMP> for a
tab and <SAMP>`\n'</SAMP> for a newline.
<PRE>
"[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*"
</PRE>
<P>
In contrast, in Lisp, you have to type the tab as Ctrl-V Ctrl-I, producing
the following:
<PRE>
sentence-end
=>
"[.?!][]\"')}]*\\($\\| $\\| \\| \\)[
]*"
</PRE>
<P>
In this output, tab and newline appear as themselves.
<P>
This regular expression contains four parts in succession and can be
deciphered as follows:
<DL COMPACT>
<DT><CODE>[.?!]</CODE>
<DD>
The first part of the pattern consists of three characters, a period, a
question mark and an exclamation mark, within square brackets. The
match must begin with one of these three characters.
<DT><CODE>[]\"')}]*</CODE>
<DD>
The second part of the pattern matches any closing braces and quotation
marks, zero or more of them, that may follow the period, question mark
or exclamation mark. The <CODE>\"</CODE> is C or Lisp syntax for a double-quote in
a string. The <SAMP>`*'</SAMP> at the end indicates that the immediately
preceding regular expression (a character set, in this case) may be
repeated zero or more times.
<DT><CODE>\\($\\| \\|\t\\| \\)</CODE>
<DD>
The third part of the pattern matches the whitespace that follows the
end of a sentence: the end of a line, or a tab, or two spaces. The
double backslashes mark the parentheses and vertical bars as regular
expression syntax; the parentheses mark the group and the vertical bars
separate alternatives. The dollar sign is used to match the end of a
line.
<DT><CODE>[ \t\n]*</CODE>
<DD>
Finally, the last part of the pattern matches any additional whitespace
beyond the minimum needed to end a sentence.
</DL>
<H2><A NAME="SEC6" HREF="regexp.html#TOC6">Common Regular Expressions Used in Editing</A></H2>
<P>
<A NAME="IDX29"></A>
<A NAME="IDX30"></A>
<P>
This section describes some common regular expressions
used for certain purposes in editing:
<P>
Page delimiter:
This is the regexp describing line-beginnings that separate pages. A good
value is <CODE>(string #\Page)</CODE>.
<P>
Paragraph separator:
This is the regular expression for recognizing the beginning of a line
that separates paragraphs. A good value is (in C syntax) <CODE>"^[
\t\f]*$"</CODE>, which is a line that consists entirely of spaces, tabs, and
form feeds.
<P>
Paragraph start:
This is the regular expression for recognizing the beginning of a line
that starts <EM>or</EM> separates paragraphs. A good value is (in C syntax)
<CODE>"^[ \t\n\f]"</CODE>, which matches a line starting with a space, tab,
newline, or form feed.
<P>
Sentence end:
This is the regular expression describing the end of a sentence. (All
paragraph boundaries also end sentences, regardless.) A good value
is (in C syntax, again):
<PRE>
"[.?!][]\"')}]*\\($\\|\t\\| \\)[ \t\n]*"
</PRE>
<P>
This means a period, question mark or exclamation mark, followed by a
closing brace, followed by tabs, spaces or new lines.
<H1><A NAME="SEC7" HREF="regexp.html#TOC7">The Regular Expression Module</A></H1>
<P>
<DL>
<DT><U>Function:</U> <B>match</B> <I>regexp string &#38;key start end</I>
<DD><A NAME="IDX31"></A>
This function returns as first value a <CODE>match</CODE> structure containing
the indices of the start and end of the first match for
the regular expression <VAR>regexp</VAR> in <VAR>string</VAR>, or <CODE>nil</CODE> if
there is no match. If <VAR>start</VAR> is non-<CODE>nil</CODE>, the search starts
at that index in <VAR>string</VAR>. If <VAR>end</VAR> is non-<CODE>nil</CODE>, only
<CODE>(subseq <VAR>string</VAR> <VAR>start</VAR> <VAR>end</VAR>)</CODE> is considered.
<P>
For example,
<PRE>
(match "quick" "The quick brown fox jumped quickly.")
=> #S(match :start 4 :end 9)
(match "quick" "The quick brown fox jumped quickly." :start 8)
=> #S(match :start 27 :end 32)
(match "quick" "The quick brown fox jumped quickly."
:start 8 :end 30)
=> nil
</PRE>
<P>
The index of the first character of the
string is 0, the index of the second character is 1, and so on.
<P>
The next values are <CODE>match</CODE> structures for every <SAMP>`\( ... \)'</SAMP>
contruct in <VAR>regexp</VAR>, in the order that the open parentheses appear
in <VAR>regexp</VAR>.
</DL>
<P>
<DL>
<DT><U>Function:</U> <B>match-start</B> <I>match</I>
<DD><A NAME="IDX32"></A>
Extracts the start index of <VAR>match</VAR>.
</DL>
<P>
<DL>
<DT><U>Function:</U> <B>match-end</B> <I>match</I>
<DD><A NAME="IDX33"></A>
Extracts the end index of <VAR>match</VAR>.
</DL>
<P>
<DL>
<DT><U>Function:</U> <B>match-string</B> <I>string match</I>
<DD><A NAME="IDX34"></A>
Extracts the substring of <VAR>string</VAR> corresponding to a given pair of
start and end indices. The result is shared with <VAR>string</VAR>. If you want
a freshly consed string, use <CODE>copy-string</CODE> or
<CODE>(coerce (match-string ...) 'simple-string)</CODE>.
</DL>
<P>
<DL>
<DT><U>Function:</U> <B>regexp-quote</B> <I>string</I>
<DD><A NAME="IDX35"></A>
This function returns a regular expression string that matches exactly
<VAR>string</VAR> and nothing else. This allows you to request an exact
string match when calling a function that wants a regular expression.
<PRE>
(regexp-quote "^The cat$")
=> "\\^The cat\\$"
</PRE>
<P>
One use of <CODE>regexp-quote</CODE> is to combine an exact string match with
context described as a regular expression.
</DL>
<P><HR><P>
This document was generated on 12 September 2001 using
<A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A>&nbsp;1.56k.
</BODY>
</HTML>