XEmacs 21.4.18 (Social Property).
[chise/xemacs-chise.git.1] / man / lispref / searching.texi
1 @c -*-texinfo-*-
2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file lispref.texi for copying conditions.
5 @setfilename ../../info/searching.info
6 @node Searching and Matching, Syntax Tables, Text, Top
7 @chapter Searching and Matching
8 @cindex searching
9
10   XEmacs provides two ways to search through a buffer for specified
11 text: exact string searches and regular expression searches.  After a
12 regular expression search, you can examine the @dfn{match data} to
13 determine which text matched the whole regular expression or various
14 portions of it.
15
16 @menu
17 * String Search::         Search for an exact match.
18 * Regular Expressions::   Describing classes of strings.
19 * Regexp Search::         Searching for a match for a regexp.
20 * POSIX Regexps::         Searching POSIX-style for the longest match.
21 * Search and Replace::    Internals of @code{query-replace}.
22 * Match Data::            Finding out which part of the text matched
23                             various parts of a regexp, after regexp search.
24 * Searching and Case::    Case-independent or case-significant searching.
25 * Standard Regexps::      Useful regexps for finding sentences, pages,...
26 @end menu
27
28   The @samp{skip-chars@dots{}} functions also perform a kind of searching.
29 @xref{Skipping Characters}.
30
31 @node String Search
32 @section Searching for Strings
33 @cindex string search
34
35   These are the primitive functions for searching through the text in a
36 buffer.  They are meant for use in programs, but you may call them
37 interactively.  If you do so, they prompt for the search string;
38 @var{limit} and @var{noerror} are set to @code{nil}, and @var{count}
39 is set to 1.
40
41 @deffn Command search-forward string &optional limit noerror count buffer
42   This function searches forward from point for an exact match for
43 @var{string}.  If successful, it sets point to the end of the occurrence
44 found, and returns the new value of point.  If no match is found, the
45 value and side effects depend on @var{noerror} (see below).
46
47   In the following example, point is initially at the beginning of the
48 line.  Then @code{(search-forward "fox")} moves point after the last
49 letter of @samp{fox}:
50
51 @example
52 @group
53 ---------- Buffer: foo ----------
54 @point{}The quick brown fox jumped over the lazy dog.
55 ---------- Buffer: foo ----------
56 @end group
57
58 @group
59 (search-forward "fox")
60      @result{} 20
61
62 ---------- Buffer: foo ----------
63 The quick brown fox@point{} jumped over the lazy dog.
64 ---------- Buffer: foo ----------
65 @end group
66 @end example
67
68   The argument @var{limit} specifies the upper bound to the search.  (It
69 must be a position in the current buffer.)  No match extending after
70 that position is accepted.  If @var{limit} is omitted or @code{nil}, it
71 defaults to the end of the accessible portion of the buffer.
72
73 @kindex search-failed
74   What happens when the search fails depends on the value of
75 @var{noerror}.  If @var{noerror} is @code{nil}, a @code{search-failed}
76 error is signaled.  If @var{noerror} is @code{t}, @code{search-forward}
77 returns @code{nil} and does nothing.  If @var{noerror} is neither
78 @code{nil} nor @code{t}, then @code{search-forward} moves point to the
79 upper bound and returns @code{nil}.  (It would be more consistent now
80 to return the new position of point in that case, but some programs
81 may depend on a value of @code{nil}.)
82
83 If @var{count} is supplied (it must be an integer), then the search is
84 repeated that many times (each time starting at the end of the previous
85 time's match).  If @var{count} is negative, the search direction is
86 backward.  If the successive searches succeed, the function succeeds,
87 moving point and returning its new value.  Otherwise the search fails.
88
89 @var{buffer} is the buffer to search in, and defaults to the current buffer.
90 @end deffn
91
92 @deffn Command search-backward string &optional limit noerror count buffer
93 This function searches backward from point for @var{string}.  It is
94 just like @code{search-forward} except that it searches backwards and
95 leaves point at the beginning of the match.
96 @end deffn
97
98 @deffn Command word-search-forward string &optional limit noerror count buffer
99 @cindex word search
100 This function searches forward from point for a ``word'' match for
101 @var{string}.  If it finds a match, it sets point to the end of the
102 match found, and returns the new value of point.
103
104 Word matching regards @var{string} as a sequence of words, disregarding
105 punctuation that separates them.  It searches the buffer for the same
106 sequence of words.  Each word must be distinct in the buffer (searching
107 for the word @samp{ball} does not match the word @samp{balls}), but the
108 details of punctuation and spacing are ignored (searching for @samp{ball
109 boy} does match @samp{ball.  Boy!}).
110
111 In this example, point is initially at the beginning of the buffer; the
112 search leaves it between the @samp{y} and the @samp{!}.
113
114 @example
115 @group
116 ---------- Buffer: foo ----------
117 @point{}He said "Please!  Find
118 the ball boy!"
119 ---------- Buffer: foo ----------
120 @end group
121
122 @group
123 (word-search-forward "Please find the ball, boy.")
124      @result{} 35
125
126 ---------- Buffer: foo ----------
127 He said "Please!  Find
128 the ball boy@point{}!"
129 ---------- Buffer: foo ----------
130 @end group
131 @end example
132
133 If @var{limit} is non-@code{nil} (it must be a position in the current
134 buffer), then it is the upper bound to the search.  The match found must
135 not extend after that position.
136
137 If @var{noerror} is @code{nil}, then @code{word-search-forward} signals
138 an error if the search fails.  If @var{noerror} is @code{t}, then it
139 returns @code{nil} instead of signaling an error.  If @var{noerror} is
140 neither @code{nil} nor @code{t}, it moves point to @var{limit} (or the
141 end of the buffer) and returns @code{nil}.
142
143 If @var{count} is non-@code{nil}, then the search is repeated that many
144 times.  Point is positioned at the end of the last match.
145
146 @var{buffer} is the buffer to search in, and defaults to the current buffer.
147 @end deffn
148
149 @deffn Command word-search-backward string &optional limit noerror count buffer
150 This function searches backward from point for a word match to
151 @var{string}.  This function is just like @code{word-search-forward}
152 except that it searches backward and normally leaves point at the
153 beginning of the match.
154 @end deffn
155
156 @node Regular Expressions
157 @section Regular Expressions
158 @cindex regular expression
159 @cindex regexp
160
161   A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
162 denotes a (possibly infinite) set of strings.  Searching for matches for
163 a regexp is a very powerful operation.  This section explains how to write
164 regexps; the following section says how to search for them.
165
166  To gain a thorough understanding of regular expressions and how to use
167 them to best advantage, we recommend that you study @cite{Mastering
168 Regular Expressions, by Jeffrey E.F. Friedl, O'Reilly and Associates,
169 1997}. (It's known as the "Hip Owls" book, because of the picture on its
170 cover.)  You might also read the manuals to @ref{(gawk)Top},
171 @ref{(ed)Top}, @cite{sed}, @cite{grep}, @ref{(perl)Top},
172 @ref{(regex)Top}, @ref{(rx)Top}, @cite{pcre}, and @ref{(flex)Top}, which
173 also make good use of regular expressions.
174
175  The XEmacs regular expression syntax most closely resembles that of
176 @cite{ed}, or @cite{grep}, the GNU versions of which all utilize the GNU
177 @cite{regex} library.  XEmacs' version of @cite{regex} has recently been
178 extended with some Perl--like capabilities, described in the next
179 section.
180
181 @menu
182 * Syntax of Regexps::       Rules for writing regular expressions.
183 * Regexp Example::          Illustrates regular expression syntax.
184 @end menu
185
186 @node Syntax of Regexps
187 @subsection Syntax of Regular Expressions
188
189   Regular expressions have a syntax in which a few characters are
190 special constructs and the rest are @dfn{ordinary}.  An ordinary
191 character is a simple regular expression that matches that character and
192 nothing else.  The special characters are @samp{.}, @samp{*}, @samp{+},
193 @samp{?}, @samp{[}, @samp{]}, @samp{^}, @samp{$}, and @samp{\}; no new
194 special characters will be defined in the future.  Any other character
195 appearing in a regular expression is ordinary, unless a @samp{\}
196 precedes it.
197
198 For example, @samp{f} is not a special character, so it is ordinary, and
199 therefore @samp{f} is a regular expression that matches the string
200 @samp{f} and no other string.  (It does @emph{not} match the string
201 @samp{ff}.)  Likewise, @samp{o} is a regular expression that matches
202 only @samp{o}.@refill
203
204 Any two regular expressions @var{a} and @var{b} can be concatenated.  The
205 result is a regular expression that matches a string if @var{a} matches
206 some amount of the beginning of that string and @var{b} matches the rest of
207 the string.@refill
208
209 As a simple example, we can concatenate the regular expressions @samp{f}
210 and @samp{o} to get the regular expression @samp{fo}, which matches only
211 the string @samp{fo}.  Still trivial.  To do something more powerful, you
212 need to use one of the special characters.  Here is a list of them:
213
214 @need 1200
215 @table @kbd
216 @item .@: @r{(Period)}
217 @cindex @samp{.} in regexp
218 is a special character that matches any single character except a newline.
219 Using concatenation, we can make regular expressions like @samp{a.b}, which
220 matches any three-character string that begins with @samp{a} and ends with
221 @samp{b}.@refill
222
223 @item *
224 @cindex @samp{*} in regexp
225 is not a construct by itself; it is a quantifying suffix operator that
226 means to repeat the preceding regular expression as many times as
227 possible.  In @samp{fo*}, the @samp{*} applies to the @samp{o}, so
228 @samp{fo*} matches one @samp{f} followed by any number of @samp{o}s.
229 The case of zero @samp{o}s is allowed: @samp{fo*} does match
230 @samp{f}.@refill
231
232 @samp{*} always applies to the @emph{smallest} possible preceding
233 expression.  Thus, @samp{fo*} has a repeating @samp{o}, not a
234 repeating @samp{fo}.@refill
235
236 The matcher processes a @samp{*} construct by matching, immediately, as
237 many repetitions as can be found; it is "greedy".  Then it continues
238 with the rest of the pattern.  If that fails, backtracking occurs,
239 discarding some of the matches of the @samp{*}-modified construct in
240 case that makes it possible to match the rest of the pattern.  For
241 example, in matching @samp{ca*ar} against the string @samp{caaar}, the
242 @samp{a*} first tries to match all three @samp{a}s; but the rest of the
243 pattern is @samp{ar} and there is only @samp{r} left to match, so this
244 try fails.  The next alternative is for @samp{a*} to match only two
245 @samp{a}s.  With this choice, the rest of the regexp matches
246 successfully.@refill
247
248 Nested repetition operators can be extremely slow if they specify
249 backtracking loops.  For example, it could take hours for the regular
250 expression @samp{\(x+y*\)*a} to match the sequence
251 @samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}.  The slowness is because
252 Emacs must try each imaginable way of grouping the 35 @samp{x}'s before
253 concluding that none of them can work.  To make sure your regular
254 expressions run fast, check nested repetitions carefully.
255
256 @item +
257 @cindex @samp{+} in regexp
258 is a quantifying suffix operator similar to @samp{*} except that the
259 preceding expression must match at least once.  It is also "greedy".
260 So, for example, @samp{ca+r} matches the strings @samp{car} and
261 @samp{caaaar} but not the string @samp{cr}, whereas @samp{ca*r} matches
262 all three strings.
263
264 @item ?
265 @cindex @samp{?} in regexp
266 is a quantifying suffix operator similar to @samp{*}, except that the
267 preceding expression can match either once or not at all.  For example,
268 @samp{ca?r} matches @samp{car} or @samp{cr}, but does not match anything
269 else.
270
271 @item *?
272 @cindex @samp{*?} in regexp
273 works just like @samp{*}, except that rather than matching the longest
274 match, it matches the shortest match.  @samp{*?} is known as a
275 @dfn{non-greedy} quantifier, a regexp construct borrowed from Perl.
276 @c Did perl get this from somewhere?  What's the real history of *? ?
277
278 This construct is very useful for when you want to match the text inside
279 a pair of delimiters.  For instance, @samp{/\*.*?\*/} will match C
280 comments in a string.  This could not easily be achieved without the use
281 of a non-greedy quantifier.
282
283 This construct has not been available prior to XEmacs 20.4.  It is not
284 available in FSF Emacs.
285
286 @item +?
287 @cindex @samp{+?} in regexp
288 is the non-greedy version of @samp{+}.
289
290 @item ??
291 @cindex @samp{??} in regexp
292 is the non-greedy version of @samp{?}.
293
294 @item \@{n,m\@}
295 @c Note the spacing after the close brace is deliberate.
296 @cindex @samp{\@{n,m\@} }in regexp
297 serves as an interval quantifier, analogous to @samp{*} or @samp{+}, but
298 specifies that the expression must match at least @var{n} times, but no
299 more than @var{m} times.  This syntax is supported by most Unix regexp
300 utilities, and has been introduced to XEmacs for the version 20.3.
301
302 Unfortunately, the non-greedy version of this quantifier does not exist
303 currently, although it does in Perl.
304
305 @item [ @dots{} ]
306 @cindex character set (in regexp)
307 @cindex @samp{[} in regexp
308 @cindex @samp{]} in regexp
309 @samp{[} begins a @dfn{character set}, which is terminated by a
310 @samp{]}.  In the simplest case, the characters between the two brackets
311 form the set.  Thus, @samp{[ad]} matches either one @samp{a} or one
312 @samp{d}, and @samp{[ad]*} matches any string composed of just @samp{a}s
313 and @samp{d}s (including the empty string), from which it follows that
314 @samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
315 @samp{caddaar}, etc.@refill
316
317 The usual regular expression special characters are not special inside a
318 character set.  A completely different set of special characters exists
319 inside character sets: @samp{]}, @samp{-} and @samp{^}.@refill
320
321 @samp{-} is used for ranges of characters.  To write a range, write two
322 characters with a @samp{-} between them.  Thus, @samp{[a-z]} matches any
323 lower case letter.  Ranges may be intermixed freely with individual
324 characters, as in @samp{[a-z$%.]}, which matches any lower case letter
325 or @samp{$}, @samp{%}, or a period.@refill
326
327 To include a @samp{]} in a character set, make it the first character.
328 For example, @samp{[]a]} matches @samp{]} or @samp{a}.  To include a
329 @samp{-}, write @samp{-} as the first character in the set, or put it
330 immediately after a range.  (You can replace one individual character
331 @var{c} with the range @samp{@var{c}-@var{c}} to make a place to put the
332 @samp{-}.)  There is no way to write a set containing just @samp{-} and
333 @samp{]}.
334
335 To include @samp{^} in a set, put it anywhere but at the beginning of
336 the set.
337
338 @item [^ @dots{} ]
339 @cindex @samp{^} in regexp
340 @samp{[^} begins a @dfn{complement character set}, which matches any
341 character except the ones specified.  Thus, @samp{[^a-z0-9A-Z]}
342 matches all characters @emph{except} letters and digits.@refill
343
344 @samp{^} is not special in a character set unless it is the first
345 character.  The character following the @samp{^} is treated as if it
346 were first (thus, @samp{-} and @samp{]} are not special there).
347
348 Note that a complement character set can match a newline, unless
349 newline is mentioned as one of the characters not to match.
350
351 @item ^
352 @cindex @samp{^} in regexp
353 @cindex beginning of line in regexp
354 is a special character that matches the empty string, but only at the
355 beginning of a line in the text being matched.  Otherwise it fails to
356 match anything.  Thus, @samp{^foo} matches a @samp{foo} that occurs at
357 the beginning of a line.
358
359 When matching a string instead of a buffer, @samp{^} matches at the
360 beginning of the string or after a newline character @samp{\n}.
361
362 @item $
363 @cindex @samp{$} in regexp
364 is similar to @samp{^} but matches only at the end of a line.  Thus,
365 @samp{x+$} matches a string of one @samp{x} or more at the end of a line.
366
367 When matching a string instead of a buffer, @samp{$} matches at the end
368 of the string or before a newline character @samp{\n}.
369
370 @item \
371 @cindex @samp{\} in regexp
372 has two functions: it quotes the special characters (including
373 @samp{\}), and it introduces additional special constructs.
374
375 Because @samp{\} quotes special characters, @samp{\$} is a regular
376 expression that matches only @samp{$}, and @samp{\[} is a regular
377 expression that matches only @samp{[}, and so on.
378
379 Note that @samp{\} also has special meaning in the read syntax of Lisp
380 strings (@pxref{String Type}), and must be quoted with @samp{\}.  For
381 example, the regular expression that matches the @samp{\} character is
382 @samp{\\}.  To write a Lisp string that contains the characters
383 @samp{\\}, Lisp syntax requires you to quote each @samp{\} with another
384 @samp{\}.  Therefore, the read syntax for a regular expression matching
385 @samp{\} is @code{"\\\\"}.@refill
386 @end table
387
388 @strong{Please note:} For historical compatibility, special characters
389 are treated as ordinary ones if they are in contexts where their special
390 meanings make no sense.  For example, @samp{*foo} treats @samp{*} as
391 ordinary since there is no preceding expression on which the @samp{*}
392 can act.  It is poor practice to depend on this behavior; quote the
393 special character anyway, regardless of where it appears.@refill
394
395 For the most part, @samp{\} followed by any character matches only
396 that character.  However, there are several exceptions: characters
397 that, when preceded by @samp{\}, are special constructs.  Such
398 characters are always ordinary when encountered on their own.  Here
399 is a table of @samp{\} constructs:
400
401 @table @kbd
402 @item \|
403 @cindex @samp{|} in regexp
404 @cindex regexp alternative
405 specifies an alternative.
406 Two regular expressions @var{a} and @var{b} with @samp{\|} in
407 between form an expression that matches anything that either @var{a} or
408 @var{b} matches.@refill
409
410 Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
411 but no other string.@refill
412
413 @samp{\|} applies to the largest possible surrounding expressions.  Only a
414 surrounding @samp{\( @dots{} \)} grouping can limit the grouping power of
415 @samp{\|}.@refill
416
417 Full backtracking capability exists to handle multiple uses of @samp{\|}.
418
419 @item \( @dots{} \)
420 @cindex @samp{(} in regexp
421 @cindex @samp{)} in regexp
422 @cindex regexp grouping
423 is a grouping construct that serves three purposes:
424
425 @enumerate
426 @item
427 To enclose a set of @samp{\|} alternatives for other operations.
428 Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.
429
430 @item
431 To enclose an expression for a suffix operator such as @samp{*} to act
432 on.  Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any
433 (zero or more) number of @samp{na} strings.@refill
434
435 @item
436 To record a matched substring for future reference.
437 @end enumerate
438
439 This last application is not a consequence of the idea of a
440 parenthetical grouping; it is a separate feature that happens to be
441 assigned as a second meaning to the same @samp{\( @dots{} \)} construct
442 because there is no conflict in practice between the two meanings.
443 Here is an explanation of this feature:
444
445 @item \@var{digit}
446 matches the same text that matched the @var{digit}th occurrence of a
447 @samp{\( @dots{} \)} construct.
448
449 In other words, after the end of a @samp{\( @dots{} \)} construct, the
450 matcher remembers the beginning and end of the text matched by that
451 construct.  Then, later on in the regular expression, you can use
452 @samp{\} followed by @var{digit} to match that same text, whatever it
453 may have been.
454
455 The strings matching the first nine @samp{\( @dots{} \)} constructs
456 appearing in a regular expression are assigned numbers 1 through 9 in
457 the order that the open parentheses appear in the regular expression.
458 So you can use @samp{\1} through @samp{\9} to refer to the text matched
459 by the corresponding @samp{\( @dots{} \)} constructs.
460
461 For example, @samp{\(.*\)\1} matches any newline-free string that is
462 composed of two identical halves.  The @samp{\(.*\)} matches the first
463 half, which may be anything, but the @samp{\1} that follows must match
464 the same exact text.
465
466 @item \(?: @dots{} \)
467 @cindex @samp{\(?:} in regexp
468 @cindex regexp grouping
469 is called a @dfn{shy} grouping operator, and it is used just like
470 @samp{\( @dots{} \)}, except that it does not cause the matched
471 substring to be recorded for future reference.
472
473 This is useful when you need a lot of grouping @samp{\( @dots{} \)}
474 constructs, but only want to remember one or two -- or if you have
475 more than nine groupings and need to use backreferences to refer to
476 the groupings at the end.  It also allows construction of regular
477 expressions from variable subexpressions that contain varying numbers of
478 non-capturing subexpressions, without disturbing the group counts for
479 the main expression.  For example
480
481 @example
482 (let ((sre (if foo "\\(?:bar\\|baz\\)" "quux")))
483   (re-search-forward (format "a\\(b+ %s c+\\) d" sre) nil t)
484   (match-string 1))
485 @end example
486
487 It is very tedious to write this kind of code without shy groups, even
488 if you know what all the alternative subexpressions will look like.
489
490 Using @samp{\(?: @dots{} \)} rather than @samp{\( @dots{} \)} should
491 give little performance gain, as the start of each group must be
492 recorded for the purpose of back-tracking in any case, and no string
493 copying is done until @code{match-string} is called.
494
495 The shy grouping operator has been borrowed from Perl, and was not
496 available prior to XEmacs 20.3, and has only been available in GNU Emacs
497 since version 21.
498
499 @item \w
500 @cindex @samp{\w} in regexp
501 matches any word-constituent character.  The editor syntax table
502 determines which characters these are.  @xref{Syntax Tables}.
503
504 @item \W
505 @cindex @samp{\W} in regexp
506 matches any character that is not a word constituent.
507
508 @item \s@var{code}
509 @cindex @samp{\s} in regexp
510 matches any character whose syntax is @var{code}.  Here @var{code} is a
511 character that represents a syntax code: thus, @samp{w} for word
512 constituent, @samp{-} for whitespace, @samp{(} for open parenthesis,
513 etc.  @xref{Syntax Tables}, for a list of syntax codes and the
514 characters that stand for them.
515
516 @item \S@var{code}
517 @cindex @samp{\S} in regexp
518 matches any character whose syntax is not @var{code}.
519
520 @item \c@var{category}
521 @cindex @samp{\c} in regexp
522 matches any character in @var{category}. Only available under Mule,
523 categories, and category tables, are further described in @ref{Category
524 Tables}. They are a mechanism for constructing classes of characters
525 that can be local to a buffer, and that do not require complicated []
526 expressions every time they are referenced.
527
528 @item \C@var{category}
529 @cindex @samp{\C} in regexp
530 matches any character outside @var{category}. @xref{Category Tables},
531 again, and note that this is only available under Mule. 
532 @end table
533
534   The following regular expression constructs match the empty string---that is,
535 they don't use up any characters---but whether they match depends on the
536 context.
537
538 @table @kbd
539 @item \`
540 @cindex @samp{\`} in regexp
541 matches the empty string, but only at the beginning
542 of the buffer or string being matched against.
543
544 @item \'
545 @cindex @samp{\'} in regexp
546 matches the empty string, but only at the end of
547 the buffer or string being matched against.
548
549 @item \=
550 @cindex @samp{\=} in regexp
551 matches the empty string, but only at point.
552 (This construct is not defined when matching against a string.)
553
554 @item \b
555 @cindex @samp{\b} in regexp
556 matches the empty string, but only at the beginning or
557 end of a word.  Thus, @samp{\bfoo\b} matches any occurrence of
558 @samp{foo} as a separate word.  @samp{\bballs?\b} matches
559 @samp{ball} or @samp{balls} as a separate word.@refill
560
561 @item \B
562 @cindex @samp{\B} in regexp
563 matches the empty string, but @emph{not} at the beginning or
564 end of a word.
565
566 @item \<
567 @cindex @samp{\<} in regexp
568 matches the empty string, but only at the beginning of a word.
569
570 @item \>
571 @cindex @samp{\>} in regexp
572 matches the empty string, but only at the end of a word.
573 @end table
574
575 @kindex invalid-regexp
576   Not every string is a valid regular expression.  For example, a string
577 with unbalanced square brackets is invalid (with a few exceptions, such
578 as @samp{[]]}), and so is a string that ends with a single @samp{\}.  If
579 an invalid regular expression is passed to any of the search functions,
580 an @code{invalid-regexp} error is signaled.
581
582 @defun regexp-quote string
583 This function returns a regular expression string that matches exactly
584 @var{string} and nothing else.  This allows you to request an exact
585 string match when calling a function that wants a regular expression.
586
587 @example
588 @group
589 (regexp-quote "^The cat$")
590      @result{} "\\^The cat\\$"
591 @end group
592 @end example
593
594 One use of @code{regexp-quote} is to combine an exact string match with
595 context described as a regular expression.  For example, this searches
596 for the string that is the value of @code{string}, surrounded by
597 whitespace:
598
599 @example
600 @group
601 (re-search-forward
602  (concat "\\s-" (regexp-quote string) "\\s-"))
603 @end group
604 @end example
605 @end defun
606
607 @node Regexp Example
608 @subsection Complex Regexp Example
609
610   Here is a complicated regexp, used by XEmacs to recognize the end of a
611 sentence together with any whitespace that follows.  It is the value of
612 the variable @code{sentence-end}.
613
614   First, we show the regexp as a string in Lisp syntax to distinguish
615 spaces from tab characters.  The string constant begins and ends with a
616 double-quote.  @samp{\"} stands for a double-quote as part of the
617 string, @samp{\\} for a backslash as part of the string, @samp{\t} for a
618 tab and @samp{\n} for a newline.
619
620 @example
621 "[.?!][]\"')@}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*"
622 @end example
623
624   In contrast, if you evaluate the variable @code{sentence-end}, you
625 will see the following:
626
627 @example
628 @group
629 sentence-end
630 @result{}
631 "[.?!][]\"')@}]*\\($\\| $\\|  \\|  \\)[
632 ]*"
633 @end group
634 @end example
635
636 @noindent
637 In this output, tab and newline appear as themselves.
638
639   This regular expression contains four parts in succession and can be
640 deciphered as follows:
641
642 @table @code
643 @item [.?!]
644 The first part of the pattern is a character set that matches any one of
645 three characters: period, question mark, and exclamation mark.  The
646 match must begin with one of these three characters.
647
648 @item []\"')@}]*
649 The second part of the pattern matches any closing braces and quotation
650 marks, zero or more of them, that may follow the period, question mark
651 or exclamation mark.  The @code{\"} is Lisp syntax for a double-quote in
652 a string.  The @samp{*} at the end indicates that the immediately
653 preceding regular expression (a character set, in this case) may be
654 repeated zero or more times.
655
656 @item \\($\\|@ $\\|\t\\|@ @ \\)
657 The third part of the pattern matches the whitespace that follows the
658 end of a sentence: the end of a line, or a tab, or two spaces.  The
659 double backslashes mark the parentheses and vertical bars as regular
660 expression syntax; the parentheses delimit a group and the vertical bars
661 separate alternatives.  The dollar sign is used to match the end of a
662 line.
663
664 @item [ \t\n]*
665 Finally, the last part of the pattern matches any additional whitespace
666 beyond the minimum needed to end a sentence.
667 @end table
668
669 @node Regexp Search
670 @section Regular Expression Searching
671 @cindex regular expression searching
672 @cindex regexp searching
673 @cindex searching for regexp
674
675   In XEmacs, you can search for the next match for a regexp either
676 incrementally or not.  Incremental search commands are described in the
677 @cite{The XEmacs Lisp Reference Manual}.  @xref{Regexp Search, , Regular Expression
678 Search, xemacs, The XEmacs Lisp Reference Manual}.  Here we describe only the search
679 functions useful in programs.  The principal one is
680 @code{re-search-forward}.
681
682 @deffn Command re-search-forward regexp &optional limit noerror count buffer
683 This function searches forward in the current buffer for a string of
684 text that is matched by the regular expression @var{regexp}.  The
685 function skips over any amount of text that is not matched by
686 @var{regexp}, and leaves point at the end of the first match found.
687 It returns the new value of point.
688
689 If @var{limit} is non-@code{nil} (it must be a position in the current
690 buffer), then it is the upper bound to the search.  No match extending
691 after that position is accepted.
692
693 What happens when the search fails depends on the value of
694 @var{noerror}.  If @var{noerror} is @code{nil}, a @code{search-failed}
695 error is signaled.  If @var{noerror} is @code{t},
696 @code{re-search-forward} does nothing and returns @code{nil}.  If
697 @var{noerror} is neither @code{nil} nor @code{t}, then
698 @code{re-search-forward} moves point to @var{limit} (or the end of the
699 buffer) and returns @code{nil}.
700
701 If @var{count} is supplied (it must be a positive number), then the
702 search is repeated that many times (each time starting at the end of the
703 previous time's match).  If these successive searches succeed, the
704 function succeeds, moving point and returning its new value.  Otherwise
705 the search fails.
706
707 In the following example, point is initially before the @samp{T}.
708 Evaluating the search call moves point to the end of that line (between
709 the @samp{t} of @samp{hat} and the newline).
710
711 @example
712 @group
713 ---------- Buffer: foo ----------
714 I read "@point{}The cat in the hat
715 comes back" twice.
716 ---------- Buffer: foo ----------
717 @end group
718
719 @group
720 (re-search-forward "[a-z]+" nil t 5)
721      @result{} 27
722
723 ---------- Buffer: foo ----------
724 I read "The cat in the hat@point{}
725 comes back" twice.
726 ---------- Buffer: foo ----------
727 @end group
728 @end example
729 @end deffn
730
731 @deffn Command re-search-backward regexp &optional limit noerror count buffer
732 This function searches backward in the current buffer for a string of
733 text that is matched by the regular expression @var{regexp}, leaving
734 point at the beginning of the first text found.
735
736 This function is analogous to @code{re-search-forward}, but they are not
737 simple mirror images.  @code{re-search-forward} finds the match whose
738 beginning is as close as possible to the starting point.  If
739 @code{re-search-backward} were a perfect mirror image, it would find the
740 match whose end is as close as possible.  However, in fact it finds the
741 match whose beginning is as close as possible.  The reason is that
742 matching a regular expression at a given spot always works from
743 beginning to end, and starts at a specified beginning position.
744
745 A true mirror-image of @code{re-search-forward} would require a special
746 feature for matching regexps from end to beginning.  It's not worth the
747 trouble of implementing that.
748 @end deffn
749
750 @defun string-match regexp string &optional start buffer
751 This function returns the index of the start of the first match for
752 the regular expression @var{regexp} in @var{string}, or @code{nil} if
753 there is no match.  If @var{start} is non-@code{nil}, the search starts
754 at that index in @var{string}.
755
756
757 Optional arg @var{buffer} controls how case folding is done (according
758 to the value of @code{case-fold-search} in @var{buffer} and
759 @var{buffer}'s case tables) and defaults to the current buffer.
760
761 For example,
762
763 @example
764 @group
765 (string-match
766  "quick" "The quick brown fox jumped quickly.")
767      @result{} 4
768 @end group
769 @group
770 (string-match
771  "quick" "The quick brown fox jumped quickly." 8)
772      @result{} 27
773 @end group
774 @end example
775
776 @noindent
777 The index of the first character of the
778 string is 0, the index of the second character is 1, and so on.
779
780 After this function returns, the index of the first character beyond
781 the match is available as @code{(match-end 0)}.  @xref{Match Data}.
782
783 @example
784 @group
785 (string-match
786  "quick" "The quick brown fox jumped quickly." 8)
787      @result{} 27
788 @end group
789
790 @group
791 (match-end 0)
792      @result{} 32
793 @end group
794 @end example
795 @end defun
796
797 @defun split-string string &optional pattern
798 This function splits @var{string} to substrings delimited by
799 @var{pattern}, and returns a list of substrings.  If @var{pattern} is
800 omitted, it defaults to @samp{[ \f\t\n\r\v]+}, which means that it
801 splits @var{string} by white--space.
802
803 @example
804 @group
805 (split-string "foo bar")
806      @result{} ("foo" "bar")
807 @end group
808
809 @group
810 (split-string "something")
811      @result{} ("something")
812 @end group
813
814 @group
815 (split-string "a:b:c" ":")
816      @result{} ("a" "b" "c")
817 @end group
818
819 @group
820 (split-string ":a::b:c" ":")
821      @result{} ("" "a" "" "b" "c")
822 @end group
823 @end example
824 @end defun
825
826 @defun split-path path
827 This function splits a search path into a list of strings.  The path
828 components are separated with the characters specified with
829 @code{path-separator}.  Under Unix, @code{path-separator} will normally
830 be @samp{:}, while under Windows, it will be @samp{;}.
831 @end defun
832
833 @defun looking-at regexp &optional buffer
834 This function determines whether the text in the current buffer directly
835 following point matches the regular expression @var{regexp}.  ``Directly
836 following'' means precisely that: the search is ``anchored'' and it can
837 succeed only starting with the first character following point.  The
838 result is @code{t} if so, @code{nil} otherwise.
839
840 This function does not move point, but it updates the match data, which
841 you can access using @code{match-beginning} and @code{match-end}.
842 @xref{Match Data}.
843
844 In this example, point is located directly before the @samp{T}.  If it
845 were anywhere else, the result would be @code{nil}.
846
847 @example
848 @group
849 ---------- Buffer: foo ----------
850 I read "@point{}The cat in the hat
851 comes back" twice.
852 ---------- Buffer: foo ----------
853
854 (looking-at "The cat in the hat$")
855      @result{} t
856 @end group
857 @end example
858 @end defun
859
860 @node POSIX Regexps
861 @section POSIX Regular Expression Searching
862
863   The usual regular expression functions do backtracking when necessary
864 to handle the @samp{\|} and repetition constructs, but they continue
865 this only until they find @emph{some} match.  Then they succeed and
866 report the first match found.
867
868   This section describes alternative search functions which perform the
869 full backtracking specified by the POSIX standard for regular expression
870 matching.  They continue backtracking until they have tried all
871 possibilities and found all matches, so they can report the longest
872 match, as required by POSIX.  This is much slower, so use these
873 functions only when you really need the longest match.
874
875   In Emacs versions prior to 19.29, these functions did not exist, and
876 the functions described above implemented full POSIX backtracking.
877
878 @deffn Command posix-search-forward regexp &optional limit noerror count buffer
879 This is like @code{re-search-forward} except that it performs the full
880 backtracking specified by the POSIX standard for regular expression
881 matching.
882 @end deffn
883
884 @deffn Command posix-search-backward regexp &optional limit noerror count buffer
885 This is like @code{re-search-backward} except that it performs the full
886 backtracking specified by the POSIX standard for regular expression
887 matching.
888 @end deffn
889
890 @defun posix-looking-at regexp &optional buffer
891 This is like @code{looking-at} except that it performs the full
892 backtracking specified by the POSIX standard for regular expression
893 matching.
894 @end defun
895
896 @defun posix-string-match regexp string &optional start buffer
897 This is like @code{string-match} except that it performs the full
898 backtracking specified by the POSIX standard for regular expression
899 matching.
900
901 Optional arg @var{buffer} controls how case folding is done (according
902 to the value of @code{case-fold-search} in @var{buffer} and
903 @var{buffer}'s case tables) and defaults to the current buffer.
904 @end defun
905
906 @ignore
907 @deffn Command delete-matching-lines regexp
908 This function is identical to @code{delete-non-matching-lines}, save
909 that it deletes what @code{delete-non-matching-lines} keeps.
910
911 In the example below, point is located on the first line of text.
912
913 @example
914 @group
915 ---------- Buffer: foo ----------
916 We hold these truths
917 to be self-evident,
918 that all men are created
919 equal, and that they are
920 ---------- Buffer: foo ----------
921 @end group
922
923 @group
924 (delete-matching-lines "the")
925      @result{} nil
926
927 ---------- Buffer: foo ----------
928 to be self-evident,
929 that all men are created
930 ---------- Buffer: foo ----------
931 @end group
932 @end example
933 @end deffn
934
935 @deffn Command flush-lines regexp
936 This function is an alias of @code{delete-matching-lines}.
937 @end deffn
938
939 @deffn Command delete-non-matching-lines regexp
940 This function deletes all lines following point which don't
941 contain a match for the regular expression @var{regexp}.
942 @end deffn
943
944 @deffn Command keep-lines regexp
945 This function is the same as @code{delete-non-matching-lines}.
946 @end deffn
947
948 @deffn Command count-matches regexp
949 This function counts the number of matches for @var{regexp} there are in
950 the current buffer following point.  It prints this number in
951 the echo area, returning the string printed.
952 @end deffn
953
954 @deffn Command how-many regexp
955 This function is an alias of @code{count-matches}.
956 @end deffn
957
958 @deffn Command list-matching-lines regexp &optional nlines
959 This function is a synonym of @code{occur}.
960 Show all lines following point containing a match for @var{regexp}.
961 Display each line with @var{nlines} lines before and after,
962 or @code{-}@var{nlines} before if @var{nlines} is negative.
963 @var{nlines} defaults to @code{list-matching-lines-default-context-lines}.
964 Interactively it is the prefix arg.
965
966 The lines are shown in a buffer named @samp{*Occur*}.
967 It serves as a menu to find any of the occurrences in this buffer.
968 @kbd{C-h m} (@code{describe-mode} in that buffer gives help.
969 @end deffn
970
971 @defopt list-matching-lines-default-context-lines
972 Default value is 0.
973 Default number of context lines to include around a @code{list-matching-lines}
974 match.  A negative number means to include that many lines before the match.
975 A positive number means to include that many lines both before and after.
976 @end defopt
977 @end ignore
978
979 @node Search and Replace
980 @section Search and Replace
981 @cindex replacement
982
983 @defun perform-replace from-string replacements query-flag regexp-flag delimited-flag &optional repeat-count map
984 This function is the guts of @code{query-replace} and related commands.
985 It searches for occurrences of @var{from-string} and replaces some or
986 all of them.  If @var{query-flag} is @code{nil}, it replaces all
987 occurrences; otherwise, it asks the user what to do about each one.
988
989 If @var{regexp-flag} is non-@code{nil}, then @var{from-string} is
990 considered a regular expression; otherwise, it must match literally.  If
991 @var{delimited-flag} is non-@code{nil}, then only replacements
992 surrounded by word boundaries are considered.
993
994 The argument @var{replacements} specifies what to replace occurrences
995 with.  If it is a string, that string is used.  It can also be a list of
996 strings, to be used in cyclic order.
997
998 If @var{repeat-count} is non-@code{nil}, it should be an integer.  Then
999 it specifies how many times to use each of the strings in the
1000 @var{replacements} list before advancing cyclicly to the next one.
1001
1002 Normally, the keymap @code{query-replace-map} defines the possible user
1003 responses for queries.  The argument @var{map}, if non-@code{nil}, is a
1004 keymap to use instead of @code{query-replace-map}.
1005 @end defun
1006
1007 @defvar query-replace-map
1008 This variable holds a special keymap that defines the valid user
1009 responses for @code{query-replace} and related functions, as well as
1010 @code{y-or-n-p} and @code{map-y-or-n-p}.  It is unusual in two ways:
1011
1012 @itemize @bullet
1013 @item
1014 The ``key bindings'' are not commands, just symbols that are meaningful
1015 to the functions that use this map.
1016
1017 @item
1018 Prefix keys are not supported; each key binding must be for a single event
1019 key sequence.  This is because the functions don't use read key sequence to
1020 get the input; instead, they read a single event and look it up ``by hand.''
1021 @end itemize
1022 @end defvar
1023
1024 Here are the meaningful ``bindings'' for @code{query-replace-map}.
1025 Several of them are meaningful only for @code{query-replace} and
1026 friends.
1027
1028 @table @code
1029 @item act
1030 Do take the action being considered---in other words, ``yes.''
1031
1032 @item skip
1033 Do not take action for this question---in other words, ``no.''
1034
1035 @item exit
1036 Answer this question ``no,'' and give up on the entire series of
1037 questions, assuming that the answers will be ``no.''
1038
1039 @item act-and-exit
1040 Answer this question ``yes,'' and give up on the entire series of
1041 questions, assuming that subsequent answers will be ``no.''
1042
1043 @item act-and-show
1044 Answer this question ``yes,'' but show the results---don't advance yet
1045 to the next question.
1046
1047 @item automatic
1048 Answer this question and all subsequent questions in the series with
1049 ``yes,'' without further user interaction.
1050
1051 @item backup
1052 Move back to the previous place that a question was asked about.
1053
1054 @item edit
1055 Enter a recursive edit to deal with this question---instead of any
1056 other action that would normally be taken.
1057
1058 @item delete-and-edit
1059 Delete the text being considered, then enter a recursive edit to replace
1060 it.
1061
1062 @item recenter
1063 Redisplay and center the window, then ask the same question again.
1064
1065 @item quit
1066 Perform a quit right away.  Only @code{y-or-n-p} and related functions
1067 use this answer.
1068
1069 @item help
1070 Display some help, then ask again.
1071 @end table
1072
1073 @node Match Data
1074 @section The Match Data
1075 @cindex match data
1076
1077   XEmacs keeps track of the positions of the start and end of segments of
1078 text found during a regular expression search.  This means, for example,
1079 that you can search for a complex pattern, such as a date in an Rmail
1080 message, and then extract parts of the match under control of the
1081 pattern.
1082
1083   Because the match data normally describe the most recent search only,
1084 you must be careful not to do another search inadvertently between the
1085 search you wish to refer back to and the use of the match data.  If you
1086 can't avoid another intervening search, you must save and restore the
1087 match data around it, to prevent it from being overwritten.
1088
1089 @menu
1090 * Simple Match Data::     Accessing single items of match data,
1091                             such as where a particular subexpression started.
1092 * Replacing Match::       Replacing a substring that was matched.
1093 * Entire Match Data::     Accessing the entire match data at once, as a list.
1094 * Saving Match Data::     Saving and restoring the match data.
1095 @end menu
1096
1097 @node Simple Match Data
1098 @subsection Simple Match Data Access
1099
1100   This section explains how to use the match data to find out what was
1101 matched by the last search or match operation.
1102
1103   You can ask about the entire matching text, or about a particular
1104 parenthetical subexpression of a regular expression.  The @var{count}
1105 argument in the functions below specifies which.  If @var{count} is
1106 zero, you are asking about the entire match.  If @var{count} is
1107 positive, it specifies which subexpression you want.
1108
1109   Recall that the subexpressions of a regular expression are those
1110 expressions grouped with escaped parentheses, @samp{\(@dots{}\)}.  The
1111 @var{count}th subexpression is found by counting occurrences of
1112 @samp{\(} from the beginning of the whole regular expression.  The first
1113 subexpression is numbered 1, the second 2, and so on.  Only regular
1114 expressions can have subexpressions---after a simple string search, the
1115 only information available is about the entire match.
1116
1117 @defun match-string count &optional in-string
1118 This function returns, as a string, the text matched in the last search
1119 or match operation.  It returns the entire text if @var{count} is zero,
1120 or just the portion corresponding to the @var{count}th parenthetical
1121 subexpression, if @var{count} is positive.  If @var{count} is out of
1122 range, or if that subexpression didn't match anything, the value is
1123 @code{nil}.
1124
1125 If the last such operation was done against a string with
1126 @code{string-match}, then you should pass the same string as the
1127 argument @var{in-string}.  Otherwise, after a buffer search or match,
1128 you should omit @var{in-string} or pass @code{nil} for it; but you
1129 should make sure that the current buffer when you call
1130 @code{match-string} is the one in which you did the searching or
1131 matching.
1132 @end defun
1133
1134 @defun match-beginning count
1135 This function returns the position of the start of text matched by the
1136 last regular expression searched for, or a subexpression of it.
1137
1138 If @var{count} is zero, then the value is the position of the start of
1139 the entire match.  Otherwise, @var{count} specifies a subexpression in
1140 the regular expression, and the value of the function is the starting
1141 position of the match for that subexpression.
1142
1143 The value is @code{nil} for a subexpression inside a @samp{\|}
1144 alternative that wasn't used in the match.
1145 @end defun
1146
1147 @defun match-end count
1148 This function is like @code{match-beginning} except that it returns the
1149 position of the end of the match, rather than the position of the
1150 beginning.
1151 @end defun
1152
1153   Here is an example of using the match data, with a comment showing the
1154 positions within the text:
1155
1156 @example
1157 @group
1158 (string-match "\\(qu\\)\\(ick\\)"
1159               "The quick fox jumped quickly.")
1160               ;0123456789
1161      @result{} 4
1162 @end group
1163
1164 @group
1165 (match-string 0 "The quick fox jumped quickly.")
1166      @result{} "quick"
1167 (match-string 1 "The quick fox jumped quickly.")
1168      @result{} "qu"
1169 (match-string 2 "The quick fox jumped quickly.")
1170      @result{} "ick"
1171 @end group
1172
1173 @group
1174 (match-beginning 1)       ; @r{The beginning of the match}
1175      @result{} 4                 ;   @r{with @samp{qu} is at index 4.}
1176 @end group
1177
1178 @group
1179 (match-beginning 2)       ; @r{The beginning of the match}
1180      @result{} 6                 ;   @r{with @samp{ick} is at index 6.}
1181 @end group
1182
1183 @group
1184 (match-end 1)             ; @r{The end of the match}
1185      @result{} 6                 ;   @r{with @samp{qu} is at index 6.}
1186
1187 (match-end 2)             ; @r{The end of the match}
1188      @result{} 9                 ;   @r{with @samp{ick} is at index 9.}
1189 @end group
1190 @end example
1191
1192   Here is another example.  Point is initially located at the beginning
1193 of the line.  Searching moves point to between the space and the word
1194 @samp{in}.  The beginning of the entire match is at the 9th character of
1195 the buffer (@samp{T}), and the beginning of the match for the first
1196 subexpression is at the 13th character (@samp{c}).
1197
1198 @example
1199 @group
1200 (list
1201   (re-search-forward "The \\(cat \\)")
1202   (match-beginning 0)
1203   (match-beginning 1))
1204     @result{} (9 9 13)
1205 @end group
1206
1207 @group
1208 ---------- Buffer: foo ----------
1209 I read "The cat @point{}in the hat comes back" twice.
1210         ^   ^
1211         9  13
1212 ---------- Buffer: foo ----------
1213 @end group
1214 @end example
1215
1216 @noindent
1217 (In this case, the index returned is a buffer position; the first
1218 character of the buffer counts as 1.)
1219
1220 @node Replacing Match
1221 @subsection Replacing the Text That Matched
1222
1223   This function replaces the text matched by the last search with
1224 @var{replacement}.
1225
1226 @cindex case in replacements
1227 @defun replace-match replacement &optional fixedcase literal string strbuffer
1228 This function replaces the text in the buffer (or in @var{string}) that
1229 was matched by the last search.  It replaces that text with
1230 @var{replacement}.
1231
1232 If you did the last search in a buffer, you should specify @code{nil}
1233 for @var{string}.  Then @code{replace-match} does the replacement by
1234 editing the buffer; it leaves point at the end of the replacement text,
1235 and returns @code{t}.
1236
1237 If you did the search in a string, pass the same string as @var{string}.
1238 Then @code{replace-match} does the replacement by constructing and
1239 returning a new string.
1240
1241 If the fourth argument @var{string} is a string, fifth argument
1242 @var{strbuffer} specifies the buffer to be used for syntax-table and
1243 case-table lookup and defaults to the current buffer.  When @var{string}
1244 is not a string, the buffer that the match occurred in has automatically
1245 been remembered and you do not need to specify it.
1246
1247 If @var{fixedcase} is non-@code{nil}, then the case of the replacement
1248 text is not changed; otherwise, the replacement text is converted to a
1249 different case depending upon the capitalization of the text to be
1250 replaced.  If the original text is all upper case, the replacement text
1251 is converted to upper case.  If the first word of the original text is
1252 capitalized, then the first word of the replacement text is capitalized.
1253 If the original text contains just one word, and that word is a capital
1254 letter, @code{replace-match} considers this a capitalized first word
1255 rather than all upper case.
1256
1257 If @code{case-replace} is @code{nil}, then case conversion is not done,
1258 regardless of the value of @var{fixedcase}.  @xref{Searching and Case}.
1259
1260 If @var{literal} is non-@code{nil}, then @var{replacement} is inserted
1261 exactly as it is, the only alterations being case changes as needed.
1262 If it is @code{nil} (the default), then the character @samp{\} is treated
1263 specially.  If a @samp{\} appears in @var{replacement}, then it must be
1264 part of one of the following sequences:
1265
1266 @table @asis
1267 @item @samp{\&}
1268 @cindex @samp{&} in replacement
1269 @samp{\&} stands for the entire text being replaced.
1270
1271 @item @samp{\@var{n}}
1272 @cindex @samp{\@var{n}} in replacement
1273 @samp{\@var{n}}, where @var{n} is a digit, stands for the text that
1274 matched the @var{n}th subexpression in the original regexp.
1275 Subexpressions are those expressions grouped inside @samp{\(@dots{}\)}.
1276
1277 @item @samp{\\}
1278 @cindex @samp{\} in replacement
1279 @samp{\\} stands for a single @samp{\} in the replacement text.
1280 @end table
1281 @end defun
1282
1283 @node Entire Match Data
1284 @subsection Accessing the Entire Match Data
1285
1286   The functions @code{match-data} and @code{set-match-data} read or
1287 write the entire match data, all at once.
1288
1289 @defun match-data &optional integers reuse
1290 This function returns a newly constructed list containing all the
1291 information on what text the last search matched.  Element zero is the
1292 position of the beginning of the match for the whole expression; element
1293 one is the position of the end of the match for the expression.  The
1294 next two elements are the positions of the beginning and end of the
1295 match for the first subexpression, and so on.  In general, element
1296 @ifinfo
1297 number 2@var{n}
1298 @end ifinfo
1299 @tex
1300 number {\mathsurround=0pt $2n$}
1301 @end tex
1302 corresponds to @code{(match-beginning @var{n})}; and
1303 element
1304 @ifinfo
1305 number 2@var{n} + 1
1306 @end ifinfo
1307 @tex
1308 number {\mathsurround=0pt $2n+1$}
1309 @end tex
1310 corresponds to @code{(match-end @var{n})}.
1311
1312 All the elements are markers or @code{nil} if matching was done on a
1313 buffer, and all are integers or @code{nil} if matching was done on a
1314 string with @code{string-match}.  However, if the optional first
1315 argument @var{integers} is non-@code{nil}, always use integers (rather
1316 than markers) to represent buffer positions.
1317
1318 If the optional second argument @var{reuse} is a list, reuse it as part
1319 of the value.  If @var{reuse} is long enough to hold all the values, and if
1320 @var{integers} is non-@code{nil}, no new lisp objects are created.
1321
1322 As always, there must be no possibility of intervening searches between
1323 the call to a search function and the call to @code{match-data} that is
1324 intended to access the match data for that search.
1325
1326 @example
1327 @group
1328 (match-data)
1329      @result{}  (#<marker at 9 in foo>
1330           #<marker at 17 in foo>
1331           #<marker at 13 in foo>
1332           #<marker at 17 in foo>)
1333 @end group
1334 @end example
1335 @end defun
1336
1337 @defun set-match-data match-list
1338 This function sets the match data from the elements of @var{match-list},
1339 which should be a list that was the value of a previous call to
1340 @code{match-data}.
1341
1342 If @var{match-list} refers to a buffer that doesn't exist, you don't get
1343 an error; that sets the match data in a meaningless but harmless way.
1344
1345 @findex store-match-data
1346 @code{store-match-data} is an alias for @code{set-match-data}.
1347 @end defun
1348
1349 @node Saving Match Data
1350 @subsection Saving and Restoring the Match Data
1351
1352   When you call a function that may do a search, you may need to save
1353 and restore the match data around that call, if you want to preserve the
1354 match data from an earlier search for later use.  Here is an example
1355 that shows the problem that arises if you fail to save the match data:
1356
1357 @example
1358 @group
1359 (re-search-forward "The \\(cat \\)")
1360      @result{} 48
1361 (foo)                   ; @r{Perhaps @code{foo} does}
1362                         ;   @r{more searching.}
1363 (match-end 0)
1364      @result{} 61              ; @r{Unexpected result---not 48!}
1365 @end group
1366 @end example
1367
1368   You can save and restore the match data with @code{save-match-data}:
1369
1370 @defspec save-match-data body@dots{}
1371 This special form executes @var{body}, saving and restoring the match
1372 data around it.
1373 @end defspec
1374
1375   You can use @code{set-match-data} together with @code{match-data} to
1376 imitate the effect of the special form @code{save-match-data}.  This is
1377 useful for writing code that can run in Emacs 18.  Here is how:
1378
1379 @example
1380 @group
1381 (let ((data (match-data)))
1382   (unwind-protect
1383       @dots{}   ; @r{May change the original match data.}
1384     (set-match-data data)))
1385 @end group
1386 @end example
1387
1388   Emacs automatically saves and restores the match data when it runs
1389 process filter functions (@pxref{Filter Functions}) and process
1390 sentinels (@pxref{Sentinels}).
1391
1392 @ignore
1393   Here is a function which restores the match data provided the buffer
1394 associated with it still exists.
1395
1396 @smallexample
1397 @group
1398 (defun restore-match-data (data)
1399 @c It is incorrect to split the first line of a doc string.
1400 @c If there's a problem here, it should be solved in some other way.
1401   "Restore the match data DATA unless the buffer is missing."
1402   (catch 'foo
1403     (let ((d data))
1404 @end group
1405       (while d
1406         (and (car d)
1407              (null (marker-buffer (car d)))
1408 @group
1409              ;; @file{match-data} @r{buffer is deleted.}
1410              (throw 'foo nil))
1411         (setq d (cdr d)))
1412       (set-match-data data))))
1413 @end group
1414 @end smallexample
1415 @end ignore
1416
1417 @node Searching and Case
1418 @section Searching and Case
1419 @cindex searching and case
1420
1421   By default, searches in Emacs ignore the case of the text they are
1422 searching through; if you specify searching for @samp{FOO}, then
1423 @samp{Foo} or @samp{foo} is also considered a match.  Regexps, and in
1424 particular character sets, are included: thus, @samp{[aB]} would match
1425 @samp{a} or @samp{A} or @samp{b} or @samp{B}.
1426
1427   If you do not want this feature, set the variable
1428 @code{case-fold-search} to @code{nil}.  Then all letters must match
1429 exactly, including case.  This is a buffer-local variable; altering the
1430 variable affects only the current buffer.  (@xref{Intro to
1431 Buffer-Local}.)  Alternatively, you may change the value of
1432 @code{default-case-fold-search}, which is the default value of
1433 @code{case-fold-search} for buffers that do not override it.
1434
1435   Note that the user-level incremental search feature handles case
1436 distinctions differently.  When given a lower case letter, it looks for
1437 a match of either case, but when given an upper case letter, it looks
1438 for an upper case letter only.  But this has nothing to do with the
1439 searching functions Lisp functions use.
1440
1441 @defopt case-replace
1442 This variable determines whether the replacement functions should
1443 preserve case.  If the variable is @code{nil}, that means to use the
1444 replacement text verbatim.  A non-@code{nil} value means to convert the
1445 case of the replacement text according to the text being replaced.
1446
1447 The function @code{replace-match} is where this variable actually has
1448 its effect.  @xref{Replacing Match}.
1449 @end defopt
1450
1451 @defopt case-fold-search
1452 This buffer-local variable determines whether searches should ignore
1453 case.  If the variable is @code{nil} they do not ignore case; otherwise
1454 they do ignore case.
1455 @end defopt
1456
1457 @defvar default-case-fold-search
1458 The value of this variable is the default value for
1459 @code{case-fold-search} in buffers that do not override it.  This is the
1460 same as @code{(default-value 'case-fold-search)}.
1461 @end defvar
1462
1463 @node Standard Regexps
1464 @section Standard Regular Expressions Used in Editing
1465 @cindex regexps used standardly in editing
1466 @cindex standard regexps used in editing
1467
1468   This section describes some variables that hold regular expressions
1469 used for certain purposes in editing:
1470
1471 @defvar page-delimiter
1472 This is the regexp describing line-beginnings that separate pages.  The
1473 default value is @code{"^\014"} (i.e., @code{"^^L"} or @code{"^\C-l"});
1474 this matches a line that starts with a formfeed character.
1475 @end defvar
1476
1477   The following two regular expressions should @emph{not} assume the
1478 match always starts at the beginning of a line; they should not use
1479 @samp{^} to anchor the match.  Most often, the paragraph commands do
1480 check for a match only at the beginning of a line, which means that
1481 @samp{^} would be superfluous.  When there is a nonzero left margin,
1482 they accept matches that start after the left margin.  In that case, a
1483 @samp{^} would be incorrect.  However, a @samp{^} is harmless in modes
1484 where a left margin is never used.
1485
1486 @defvar paragraph-separate
1487 This is the regular expression for recognizing the beginning of a line
1488 that separates paragraphs.  (If you change this, you may have to
1489 change @code{paragraph-start} also.)  The default value is
1490 @w{@code{"[@ \t\f]*$"}}, which matches a line that consists entirely of
1491 spaces, tabs, and form feeds (after its left margin).
1492 @end defvar
1493
1494 @defvar paragraph-start
1495 This is the regular expression for recognizing the beginning of a line
1496 that starts @emph{or} separates paragraphs.  The default value is
1497 @w{@code{"[@ \t\n\f]"}}, which matches a line starting with a space, tab,
1498 newline, or form feed (after its left margin).
1499 @end defvar
1500
1501 @defvar sentence-end
1502 This is the regular expression describing the end of a sentence.  (All
1503 paragraph boundaries also end sentences, regardless.)  The default value
1504 is:
1505
1506 @example
1507 "[.?!][]\"')@}]*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1508 @end example
1509
1510 This means a period, question mark or exclamation mark, followed
1511 optionally by a closing parenthetical character, followed by tabs,
1512 spaces or new lines.
1513
1514 For a detailed explanation of this regular expression, see @ref{Regexp
1515 Example}.
1516 @end defvar