aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2001-09-06 19:46:04 +0000
committerRichard M. Stallman2001-09-06 19:46:04 +0000
commita5d0a32e01523b1fd906bc36b62e2e3437e5f8cc (patch)
treed79a60f5a8504a4abdc9e7b390d7c52a150bbb6e
parent2a2048f2e24a87da51be10a06ccdff425eae4c51 (diff)
downloademacs-a5d0a32e01523b1fd906bc36b62e2e3437e5f8cc.tar.gz
emacs-a5d0a32e01523b1fd906bc36b62e2e3437e5f8cc.zip
Explain clearly what \digit does when that grouping
did not match.
-rw-r--r--lispref/searching.texi42
1 files changed, 27 insertions, 15 deletions
diff --git a/lispref/searching.texi b/lispref/searching.texi
index 4f0177592f5..a014080d845 100644
--- a/lispref/searching.texi
+++ b/lispref/searching.texi
@@ -548,25 +548,35 @@ numbering of any ordinary, non-shy groups.
548 548
549@item \@var{digit} 549@item \@var{digit}
550matches the same text that matched the @var{digit}th occurrence of a 550matches the same text that matched the @var{digit}th occurrence of a
551@samp{\( @dots{} \)} construct. 551grouping (@samp{\( @dots{} \)}) construct.
552 552
553In other words, after the end of a @samp{\( @dots{} \)} construct, the 553In other words, after the end of a group, the matcher remembers the
554matcher remembers the beginning and end of the text matched by that 554beginning and end of the text matched by that group. Later on in the
555construct. Then, later on in the regular expression, you can use 555regular expression you can use @samp{\} followed by @var{digit} to
556@samp{\} followed by @var{digit} to match that same text, whatever it 556match that same text, whatever it may have been.
557may have been.
558 557
559The strings matching the first nine @samp{\( @dots{} \)} constructs 558The strings matching the first nine grouping constructs appearing in
560appearing in a regular expression are assigned numbers 1 through 9 in 559the entire regular expression passed to a search or matching function
561the order that the open parentheses appear in the regular expression. 560are assigned numbers 1 through 9 in the order that the open
562So you can use @samp{\1} through @samp{\9} to refer to the text matched 561parentheses appear in the regular expression. So you can use
563by the corresponding @samp{\( @dots{} \)} constructs. 562@samp{\1} through @samp{\9} to refer to the text matched by the
563corresponding grouping constructs.
564 564
565For example, @samp{\(.*\)\1} matches any newline-free string that is 565For example, @samp{\(.*\)\1} matches any newline-free string that is
566composed of two identical halves. The @samp{\(.*\)} matches the first 566composed of two identical halves. The @samp{\(.*\)} matches the first
567half, which may be anything, but the @samp{\1} that follows must match 567half, which may be anything, but the @samp{\1} that follows must match
568the same exact text. 568the same exact text.
569 569
570If a particular grouping construct in the regular expression was never
571matched---for instance, if it appears inside of an alternative that
572wasn't used, or inside of a repetition that repeated zero times---then
573the corresponding @samp{\@var{digit}} construct never matches
574anything. To use an artificial example,, @samp{\(foo\(b*\)\|lose\)\2}
575cannot match @samp{lose}: the second alternative inside the larger
576group matches it, but then @samp{\2} is undefined and can't match
577anything. But it can match @samp{foobb}, because the first
578alternative matches @samp{foob} and @samp{\2} matches @samp{b}.
579
570@item \w 580@item \w
571@cindex @samp{\w} in regexp 581@cindex @samp{\w} in regexp
572matches any word-constituent character. The editor syntax table 582matches any word-constituent character. The editor syntax table
@@ -1266,9 +1276,7 @@ future.
1266This function returns, as a string, the text matched in the last search 1276This function returns, as a string, the text matched in the last search
1267or match operation. It returns the entire text if @var{count} is zero, 1277or match operation. It returns the entire text if @var{count} is zero,
1268or just the portion corresponding to the @var{count}th parenthetical 1278or just the portion corresponding to the @var{count}th parenthetical
1269subexpression, if @var{count} is positive. If @var{count} is out of 1279subexpression, if @var{count} is positive.
1270range, or if that subexpression didn't match anything, the value is
1271@code{nil}.
1272 1280
1273If the last such operation was done against a string with 1281If the last such operation was done against a string with
1274@code{string-match}, then you should pass the same string as the 1282@code{string-match}, then you should pass the same string as the
@@ -1277,6 +1285,10 @@ you should omit @var{in-string} or pass @code{nil} for it; but you
1277should make sure that the current buffer when you call 1285should make sure that the current buffer when you call
1278@code{match-string} is the one in which you did the searching or 1286@code{match-string} is the one in which you did the searching or
1279matching. 1287matching.
1288
1289The value is @code{nil} if @var{count} is out of range, or for a
1290subexpression inside a @samp{\|} alternative that wasn't used or a
1291repetition that repeated zero times.
1280@end defun 1292@end defun
1281 1293
1282@defun match-string-no-properties count &optional in-string 1294@defun match-string-no-properties count &optional in-string
@@ -1294,7 +1306,7 @@ the regular expression, and the value of the function is the starting
1294position of the match for that subexpression. 1306position of the match for that subexpression.
1295 1307
1296The value is @code{nil} for a subexpression inside a @samp{\|} 1308The value is @code{nil} for a subexpression inside a @samp{\|}
1297alternative that wasn't used in the match. 1309alternative that wasn't used or a repetition that repeated zero times.
1298@end defun 1310@end defun
1299 1311
1300@defun match-end count 1312@defun match-end count