This commit was generated by cvs2svn to compensate for changes in r1705,
[chise/xemacs-chise.git.1] / man / xemacs / search.texi
index ab72126..ea892d0 100644 (file)
@@ -77,7 +77,7 @@ only if the next command you want to type is a printing character,
 within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, or @kbd{C-y}).
 
   Sometimes you search for @samp{FOO} and find it, but were actually
-looking for a different occurence of it.  To move to the next occurrence
+looking for a different occurrence of it.  To move to the next occurrence
 of the search string, type another @kbd{C-s}.  Do this as often as
 necessary.  If you overshoot, you can cancel some @kbd{C-s}
 characters with @key{DEL}.
@@ -330,12 +330,30 @@ is the traditional Emacs sequence of keys for word search.
 @cindex regexp
 
   A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
-denotes a set of strings, possibly an infinite set.  Searching for matches
+denotes a (possibly infinite) set of strings.  Searching for matches
 for a regexp is a powerful operation that editors on Unix systems have
-traditionally offered.  In XEmacs, you can search for the next match for
-a regexp either incrementally or not.
+traditionally offered.
+
+ To gain a thorough understanding of regular expressions and how to use
+them to best advantage, we recommend that you study @cite{Mastering
+Regular Expressions, by Jeffrey E.F. Friedl, O'Reilly and Associates,
+1997}. (It's known as the "Hip Owls" book, because of the picture on its
+cover.)  You might also read the manuals to @ref{(gawk)Top},
+@ref{(ed)Top}, @cite{sed}, @cite{grep}, @ref{(perl)Top},
+@ref{(regex)Top}, @ref{(rx)Top}, @cite{pcre}, and @ref{(flex)Top}, which
+also make good use of regular expressions.
+
+ The XEmacs regular expression syntax most closely resembles that of
+@cite{ed}, or @cite{grep}, the GNU versions of which all utilize the GNU
+@cite{regex} library.  XEmacs' version of @cite{regex} has recently been
+extended with some Perl--like capabilities, described in the next
+section.
+
+ In XEmacs, you can search for the next match for a regexp either
+incrementally or not.
 
 @kindex M-C-s
+@kindex M-C-r
 @findex isearch-forward-regexp
 @findex isearch-backward-regexp
   Incremental search for a regexp is done by typing @kbd{M-C-s}
@@ -344,7 +362,7 @@ incrementally just like @kbd{C-s}, but it treats the search string as a
 regexp rather than looking for an exact match against the text in the
 buffer.  Each time you add text to the search string, you make the regexp
 longer, and the new regexp is searched for.  A reverse regexp search command
-@code{isearch-backward-regexp} also exists, but no key runs it.
+@code{isearch-backward-regexp} also exists, bound to @kbd{M-C-r}.
 
   All of the control characters that do special things within an ordinary
 incremental search have the same functionality in incremental regexp search.
@@ -358,151 +376,227 @@ incremental regexp and non-regexp searches have independent defaults.
 @code{re-search-forward} and @code{re-search-backward}.  You can invoke
 them with @kbd{M-x} or bind them to keys.  You can also call
 @code{re-search-forward} by way of incremental regexp search with
-@kbd{M-C-s @key{RET}}.
+@kbd{M-C-s @key{RET}}; similarly for @code{re-search-backward} with
+@kbd{M-C-r @key{RET}}.
 
 @node Regexps, Search Case, Regexp Search, Search
 @section Syntax of Regular Expressions
 
-Regular expressions have a syntax in which a few characters are special
-constructs and the rest are @dfn{ordinary}.  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{]} and @samp{\}; no new special
-characters will be defined.  Any other character appearing in a regular
-expression is ordinary, unless a @samp{\} precedes it.@refill
+  Regular expressions have a syntax in which a few characters are
+special constructs and the rest are @dfn{ordinary}.  An ordinary
+character is a simple regular expression that matches that character and
+nothing else.  The special characters are @samp{.}, @samp{*}, @samp{+},
+@samp{?}, @samp{[}, @samp{]}, @samp{^}, @samp{$}, and @samp{\}; no new
+special characters will be defined in the future.  Any other character
+appearing in a regular expression is ordinary, unless a @samp{\}
+precedes it.
 
 For example, @samp{f} is not a special character, so it is ordinary, and
-therefore @samp{f} is a regular expression that matches the string @samp{f}
-and no other string.  (It does @i{not} match the string @samp{ff}.)  Likewise,
-@samp{o} is a regular expression that matches only @samp{o}.@refill
+therefore @samp{f} is a regular expression that matches the string
+@samp{f} and no other string.  (It does @emph{not} match the string
+@samp{ff}.)  Likewise, @samp{o} is a regular expression that matches
+only @samp{o}.@refill
 
 Any two regular expressions @var{a} and @var{b} can be concatenated.  The
-result is a regular expression which matches a string if @var{a} matches
+result is a regular expression that matches a string if @var{a} matches
 some amount of the beginning of that string and @var{b} matches the rest of
 the string.@refill
 
-As a simple example, you can concatenate the regular expressions @samp{f}
+As a simple example, we can concatenate the regular expressions @samp{f}
 and @samp{o} to get the regular expression @samp{fo}, which matches only
-the string @samp{fo}.  To do something nontrivial, you
-need to use one of the following special characters:
+the string @samp{fo}.  Still trivial.  To do something more powerful, you
+need to use one of the special characters.  Here is a list of them:
 
+@need 1200
 @table @kbd
 @item .@: @r{(Period)}
+@cindex @samp{.} in regexp
 is a special character that matches any single character except a newline.
-Using concatenation, you can make regular expressions like @samp{a.b}, which
-matches any three-character string which begins with @samp{a} and ends with
+Using concatenation, we can make regular expressions like @samp{a.b}, which
+matches any three-character string that begins with @samp{a} and ends with
 @samp{b}.@refill
 
 @item *
-is not a construct by itself; it is a suffix, which means the
-preceding regular expression is to be repeated as many times as
+@cindex @samp{*} in regexp
+is not a construct by itself; it is a quantifying suffix operator that
+means to repeat the preceding regular expression as many times as
 possible.  In @samp{fo*}, the @samp{*} applies to the @samp{o}, so
 @samp{fo*} matches one @samp{f} followed by any number of @samp{o}s.
 The case of zero @samp{o}s is allowed: @samp{fo*} does match
 @samp{f}.@refill
 
-@samp{*} always applies to the @i{smallest} possible preceding
+@samp{*} always applies to the @emph{smallest} possible preceding
 expression.  Thus, @samp{fo*} has a repeating @samp{o}, not a
 repeating @samp{fo}.@refill
 
-The matcher processes a @samp{*} construct by immediately matching
-as many repetitions as it can find.  Then it continues with the rest
-of the pattern.  If that fails, backtracking occurs, discarding some
-of the matches of the @samp{*}-modified construct in case that makes
-it possible to match the rest of the pattern.  For example, matching
-@samp{ca*ar} against the string @samp{caaar}, the @samp{a*} first
-tries to match all three @samp{a}s; but the rest of the pattern is
-@samp{ar} and there is only @samp{r} left to match, so this try fails.
-The next alternative is for @samp{a*} to match only two @samp{a}s.
-With this choice, the rest of the regexp matches successfully.@refill
+The matcher processes a @samp{*} construct by matching, immediately, as
+many repetitions as can be found; it is "greedy".  Then it continues
+with the rest of the pattern.  If that fails, backtracking occurs,
+discarding some of the matches of the @samp{*}-modified construct in
+case that makes it possible to match the rest of the pattern.  For
+example, in matching @samp{ca*ar} against the string @samp{caaar}, the
+@samp{a*} first tries to match all three @samp{a}s; but the rest of the
+pattern is @samp{ar} and there is only @samp{r} left to match, so this
+try fails.  The next alternative is for @samp{a*} to match only two
+@samp{a}s.  With this choice, the rest of the regexp matches
+successfully.@refill
+
+Nested repetition operators can be extremely slow if they specify
+backtracking loops.  For example, it could take hours for the regular
+expression @samp{\(x+y*\)*a} to match the sequence
+@samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}.  The slowness is because
+Emacs must try each imaginable way of grouping the 35 @samp{x}'s before
+concluding that none of them can work.  To make sure your regular
+expressions run fast, check nested repetitions carefully.
 
 @item +
-is a suffix character similar to @samp{*} except that it requires that
-the preceding expression be matched at least once.  For example,
-@samp{ca+r} will match the strings @samp{car} and @samp{caaaar}
-but not the string @samp{cr}, whereas @samp{ca*r} would match all
-three strings.@refill
+@cindex @samp{+} in regexp
+is a quantifying suffix operator similar to @samp{*} except that the
+preceding expression must match at least once.  It is also "greedy".
+So, for example, @samp{ca+r} matches the strings @samp{car} and
+@samp{caaaar} but not the string @samp{cr}, whereas @samp{ca*r} matches
+all three strings.
 
 @item ?
-is a suffix character similar to @samp{*} except that it can match the
-preceding expression either once or not at all.  For example,
-@samp{ca?r} will match @samp{car} or @samp{cr}; nothing else.
+@cindex @samp{?} in regexp
+is a quantifying suffix operator similar to @samp{*}, except that the
+preceding expression can match either once or not at all.  For example,
+@samp{ca?r} matches @samp{car} or @samp{cr}, but does not match anything
+else.
+
+@item *?
+@cindex @samp{*?} in regexp
+works just like @samp{*}, except that rather than matching the longest
+match, it matches the shortest match.  @samp{*?} is known as a
+@dfn{non-greedy} quantifier, a regexp construct borrowed from Perl.
+@c Did perl get this from somewhere?  What's the real history of *? ?
+
+This construct is very useful for when you want to match the text inside
+a pair of delimiters.  For instance, @samp{/\*.*?\*/} will match C
+comments in a string.  This could not easily be achieved without the use
+of a non-greedy quantifier.
+
+This construct has not been available prior to XEmacs 20.4.  It is not
+available in FSF Emacs.
+
+@item +?
+@cindex @samp{+?} in regexp
+is the non-greedy version of @samp{+}.
+
+@item ??
+@cindex @samp{??} in regexp
+is the non-greedy version of @samp{?}.
+
+@item \@{n,m\@}
+@c Note the spacing after the close brace is deliberate.
+@cindex @samp{\@{n,m\@} }in regexp
+serves as an interval quantifier, analogous to @samp{*} or @samp{+}, but
+specifies that the expression must match at least @var{n} times, but no
+more than @var{m} times.  This syntax is supported by most Unix regexp
+utilities, and has been introduced to XEmacs for the version 20.3.
+
+Unfortunately, the non-greedy version of this quantifier does not exist
+currently, although it does in Perl.
 
 @item [ @dots{} ]
+@cindex character set (in regexp)
+@cindex @samp{[} in regexp
+@cindex @samp{]} in regexp
 @samp{[} begins a @dfn{character set}, which is terminated by a
-@samp{]}.  In the simplest case, the characters between the two form
-the set.  Thus, @samp{[ad]} matches either one @samp{a} or one
-@samp{d}, and @samp{[ad]*} matches any string composed of just
-@samp{a}s and @samp{d}s (including the empty string), from which it
-follows that @samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
+@samp{]}.  In the simplest case, the characters between the two brackets
+form the set.  Thus, @samp{[ad]} matches either one @samp{a} or one
+@samp{d}, and @samp{[ad]*} matches any string composed of just @samp{a}s
+and @samp{d}s (including the empty string), from which it follows that
+@samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
 @samp{caddaar}, etc.@refill
 
-You can include character ranges in a character set by writing two
+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{-} and @samp{^}.@refill
+
+@samp{-} is used for ranges of characters.  To write a range, write two
 characters with a @samp{-} between them.  Thus, @samp{[a-z]} matches any
-lower-case letter.  Ranges may be intermixed freely with individual
-characters, as in @samp{[a-z$%.]}, which matches any lower-case letter
-or @samp{$}, @samp{%}, or period.
-@refill
+lower case letter.  Ranges may be intermixed freely with individual
+characters, as in @samp{[a-z$%.]}, which matches any lower case letter
+or @samp{$}, @samp{%}, or a period.@refill
 
-Note that inside a character set the usual special characters are not
-special any more.  A completely different set of special characters
-exists inside character sets: @samp{]}, @samp{-}, and @samp{^}.@refill
+To include a @samp{]} in a character set, make it the first character.
+For example, @samp{[]a]} matches @samp{]} or @samp{a}.  To include a
+@samp{-}, write @samp{-} as the first character in the set, or put it
+immediately after a range.  (You can replace one individual character
+@var{c} with the range @samp{@var{c}-@var{c}} to make a place to put the
+@samp{-}.)  There is no way to write a set containing just @samp{-} and
+@samp{]}.
 
-To include a @samp{]} in a character set, you must make it the first
-character.  For example, @samp{[]a]} matches @samp{]} or @samp{a}.  To
-include a @samp{-}, write @samp{---}, which is a range containing only
-@samp{-}.  To include @samp{^}, make it other than the first character
-in the set.@refill
+To include @samp{^} in a set, put it anywhere but at the beginning of
+the set.
 
 @item [^ @dots{} ]
+@cindex @samp{^} in regexp
 @samp{[^} begins a @dfn{complement character set}, which matches any
 character except the ones specified.  Thus, @samp{[^a-z0-9A-Z]}
-matches all characters @i{except} letters and digits.@refill
+matches all characters @emph{except} letters and digits.@refill
 
 @samp{^} is not special in a character set unless it is the first
 character.  The character following the @samp{^} is treated as if it
-were first (@samp{-} and @samp{]} are not special there).
+were first (thus, @samp{-} and @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.
 
 @item ^
-is a special character that matches the empty string, but only if at
-the beginning of a line in the text being matched.  Otherwise, it fails
-to match anything.  Thus, @samp{^foo} matches a @samp{foo} that occurs
-at the beginning of a line.
+@cindex @samp{^} in regexp
+@cindex beginning of line in regexp
+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} matches a @samp{foo} that occurs at
+the beginning of a line.
+
+When matching a string instead of a buffer, @samp{^} matches at the
+beginning of the string or after a newline character @samp{\n}.
 
 @item $
+@cindex @samp{$} in regexp
 is similar to @samp{^} but matches only at the end of a line.  Thus,
-@samp{xx*$} matches a string of one @samp{x} or more at the end of a line.
+@samp{x+$} matches a string of one @samp{x} or more at the end of a line.
+
+When matching a string instead of a buffer, @samp{$} matches at the end
+of the string or before a newline character @samp{\n}.
 
 @item \
-does two things: it quotes the special characters (including
+@cindex @samp{\} in regexp
+has two functions: it quotes the special characters (including
 @samp{\}), and it introduces additional special constructs.
 
 Because @samp{\} quotes special characters, @samp{\$} is a regular
 expression that matches only @samp{$}, and @samp{\[} is a regular
-expression that matches only @samp{[}, and so on.@refill
+expression that matches only @samp{[}, and so on.
+
+@c Removed a paragraph here in lispref about doubling backslashes inside
+@c of Lisp strings.
+
 @end table
 
-Note: for historical compatibility, special characters are treated as
-ordinary ones if they are in contexts where their special meanings make no
-sense.  For example, @samp{*foo} treats @samp{*} as ordinary since there is
-no preceding expression on which the @samp{*} can act.  It is poor practice
-to depend on this behavior; better to quote the special character anyway,
-regardless of where is appears.@refill
+@strong{Please note:} For historical compatibility, special characters
+are treated as ordinary ones if they are in contexts where their special
+meanings make no sense.  For example, @samp{*foo} treats @samp{*} as
+ordinary since there is no preceding expression on which the @samp{*}
+can act.  It is poor practice to depend on this behavior; quote the
+special character anyway, regardless of where it appears.@refill
 
-Usually, @samp{\} followed by any character matches only
+For the most part, @samp{\} followed by any character matches only
 that character.  However, there are several exceptions: characters
-which, when preceded by @samp{\}, are special constructs.  Such
+that, when preceded by @samp{\}, are special constructs.  Such
 characters are always ordinary when encountered on their own.  Here
-is a table of @samp{\} constructs.
+is a table of @samp{\} constructs:
 
 @table @kbd
 @item \|
+@cindex @samp{|} in regexp
+@cindex regexp alternative
 specifies an alternative.
 Two regular expressions @var{a} and @var{b} with @samp{\|} in
-between form an expression that matches anything @var{a} or
+between form an expression that matches anything that either @var{a} or
 @var{b} matches.@refill
 
 Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
@@ -515,6 +609,9 @@ surrounding @samp{\( @dots{} \)} grouping can limit the grouping power of
 Full backtracking capability exists to handle multiple uses of @samp{\|}.
 
 @item \( @dots{} \)
+@cindex @samp{(} in regexp
+@cindex @samp{)} in regexp
+@cindex regexp grouping
 is a grouping construct that serves three purposes:
 
 @enumerate
@@ -523,78 +620,126 @@ To enclose a set of @samp{\|} alternatives for other operations.
 Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.
 
 @item
-To enclose a complicated expression for the postfix @samp{*} to operate on.
-Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any (zero or
-more) number of @samp{na} strings.@refill
+To enclose an expression for a suffix operator such as @samp{*} to act
+on.  Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any
+(zero or more) number of @samp{na} strings.@refill
 
 @item
-To mark a matched substring for future reference.
-
+To record a matched substring for future reference.
 @end enumerate
 
 This last application is not a consequence of the idea of a
-parenthetical grouping; it is a separate feature which happens to be
+parenthetical grouping; it is a separate feature that happens to be
 assigned as a second meaning to the same @samp{\( @dots{} \)} construct
-because in practice there is no conflict between the two meanings.
-Here is an explanation:
+because there is no conflict in practice between the two meanings.
+Here is an explanation of this feature:
 
 @item \@var{digit}
-after the end of a @samp{\( @dots{} \)} 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{\} followed by @var{digit}
-to mean ``match the same text matched the @var{digit}'th time by the
-@samp{\( @dots{} \)} construct.''@refill
-
-The strings matching the first nine @samp{\( @dots{} \)} constructs appearing
-in a regular expression are assigned numbers 1 through 9 in order that the
-open-parentheses appear in the regular expression.  @samp{\1} through
-@samp{\9} may be used to refer to the text matched by the corresponding
+matches the same text that matched the @var{digit}th occurrence of a
 @samp{\( @dots{} \)} construct.
 
+In other words, after the end of a @samp{\( @dots{} \)} 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{\} followed by @var{digit} to match that same text, whatever it
+may have been.
+
+The strings matching the first nine @samp{\( @dots{} \)} 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} through @samp{\9} to refer to the text matched
+by the corresponding @samp{\( @dots{} \)} constructs.
+
 For example, @samp{\(.*\)\1} matches any newline-free string that is
 composed of two identical halves.  The @samp{\(.*\)} matches the first
 half, which may be anything, but the @samp{\1} that follows must match
 the same exact text.
 
+@item \(?: @dots{} \)
+@cindex @samp{\(?:} in regexp
+@cindex regexp grouping
+is called a @dfn{shy} grouping operator, and it is used just like
+@samp{\( @dots{} \)}, except that it does not cause the matched
+substring to be recorded for future reference.
+
+This is useful when you need a lot of grouping @samp{\( @dots{} \)}
+constructs, but only want to remember one or two -- or if you have
+more than nine groupings and need to use backreferences to refer to
+the groupings at the end.
+
+Using @samp{\(?: @dots{} \)} rather than @samp{\( @dots{} \)} when you
+don't need the captured substrings ought to speed up your programs some,
+since it shortens the code path followed by the regular expression
+engine, as well as the amount of memory allocation and string copying it
+must do.  The actual performance gain to be observed has not been
+measured or quantified as of this writing.
+@c This is used to good advantage by the font-locking code, and by
+@c `regexp-opt.el'.
+
+The shy grouping operator has been borrowed from Perl, and has not been
+available prior to XEmacs 20.3, nor is it available in FSF Emacs.
+
+@item \w
+@cindex @samp{\w} in regexp
+matches any word-constituent character.  The editor syntax table
+determines which characters these are.  @xref{Syntax}.
+
+@item \W
+@cindex @samp{\W} in regexp
+matches any character that is not a word constituent.
+
+@item \s@var{code}
+@cindex @samp{\s} in regexp
+matches any character whose syntax is @var{code}.  Here @var{code} is a
+character that represents a syntax code: thus, @samp{w} for word
+constituent, @samp{-} for whitespace, @samp{(} for open parenthesis,
+etc.  @xref{Syntax}, for a list of syntax codes and the characters that
+stand for them.
+
+@item \S@var{code}
+@cindex @samp{\S} in regexp
+matches any character whose syntax is not @var{code}.
+@end table
+
+  The following regular expression constructs match the empty string---that is,
+they don't use up any characters---but whether they match depends on the
+context.
+
+@table @kbd
 @item \`
-matches the empty string, provided it is at the beginning
-of the buffer.
+@cindex @samp{\`} in regexp
+matches the empty string, but only at the beginning
+of the buffer or string being matched against.
 
 @item \'
-matches the empty string, provided it is at the end of
-the buffer.
+@cindex @samp{\'} in regexp
+matches the empty string, but only at the end of
+the buffer or string being matched against.
+
+@item \=
+@cindex @samp{\=} in regexp
+matches the empty string, but only at point.
+(This construct is not defined when matching against a string.)
 
 @item \b
-matches the empty string, provided it is at the beginning or
+@cindex @samp{\b} in regexp
+matches the empty string, but only at the beginning or
 end of a word.  Thus, @samp{\bfoo\b} matches any occurrence of
 @samp{foo} as a separate word.  @samp{\bballs?\b} matches
 @samp{ball} or @samp{balls} as a separate word.@refill
 
 @item \B
-matches the empty string, provided it is @i{not} at the beginning or
+@cindex @samp{\B} in regexp
+matches the empty string, but @emph{not} at the beginning or
 end of a word.
 
 @item \<
-matches the empty string, provided it is at the beginning of a word.
+@cindex @samp{\<} in regexp
+matches the empty string, but only at the beginning of a word.
 
 @item \>
-matches the empty string, provided it is at the end of a word.
-
-@item \w
-matches any word-constituent character.  The editor syntax table
-determines which characters these are.
-
-@item \W
-matches any character that is not a word-constituent.
-
-@item \s@var{code}
-matches any character whose syntax is @var{code}.  @var{code} is a
-character which represents a syntax code: thus, @samp{w} for word
-constituent, @samp{-} for whitespace, @samp{(} for open-parenthesis,
-etc.  @xref{Syntax}.@refill
-
-@item \S@var{code}
-matches any character whose syntax is not @var{code}.
+@cindex @samp{\>} in regexp
+matches the empty string, but only at the end of a word.
 @end table
 
   Here is a complicated regexp used by Emacs to recognize the end of a