]> code.delx.au - gnu-emacs/blobdiff - doc/lispref/searching.texi
* doc/lispref/searching.texi (Regexp Backslash): Add index entry.
[gnu-emacs] / doc / lispref / searching.texi
index 48780d0a348b7dc97937b36e83486bed15b80120..5bda1940b511dfc10d06d59444830fbc7600d8f5 100644 (file)
@@ -1,7 +1,7 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001,
-@c   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
+@c Copyright (C) 1990-1995, 1998-1999, 2001-2012
+@c   Free Software Foundation, Inc.
 @c See the file elisp.texi for copying conditions.
 @setfilename ../../info/searching
 @node Searching and Matching, Syntax Tables, Non-ASCII Characters, Top
@@ -22,7 +22,7 @@ portions of it.
 * POSIX Regexps::         Searching POSIX-style for the longest match.
 * Match Data::            Finding out which part of the text matched,
                             after a string or regexp search.
-* Search and Replace::   Commands that loop, searching and replacing.
+* Search and Replace::    Commands that loop, searching and replacing.
 * Standard Regexps::      Useful regexps for finding sentences, pages,...
 @end menu
 
@@ -49,7 +49,6 @@ This function searches forward from point for an exact match for
 @var{string}.  If successful, it sets point to the end of the occurrence
 found, and returns the new value of point.  If no match is found, the
 value and side effects depend on @var{noerror} (see below).
-@c Emacs 19 feature
 
 In the following example, point is initially at the beginning of the
 line.  Then @code{(search-forward "fox")} moves point after the last
@@ -91,18 +90,21 @@ The argument @var{noerror} only affects valid searches which fail to
 find a match.  Invalid arguments cause errors regardless of
 @var{noerror}.
 
-If @var{repeat} is supplied (it must be a positive number), then the
-search is repeated that many times (each time starting at the end of the
-previous time's match).  If these successive searches succeed, the
-function succeeds, moving point and returning its new value.  Otherwise
-the search fails, with results depending on the value of
-@var{noerror}, as described above.
+If @var{repeat} is a positive number @var{n}, it serves as a repeat
+count: the search is repeated @var{n} times, each time starting at the
+end of the previous time's match.  If these successive searches
+succeed, the function succeeds, moving point and returning its new
+value.  Otherwise the search fails, with results depending on the
+value of @var{noerror}, as described above.  If @var{repeat} is a
+negative number -@var{n}, it serves as a repeat count of @var{n} for a
+search in the opposite (backward) direction.
 @end deffn
 
 @deffn Command search-backward string &optional limit noerror repeat
 This function searches backward from point for @var{string}.  It is
-just like @code{search-forward} except that it searches backwards and
-leaves point at the beginning of the match.
+like @code{search-forward}, except that it searches backwards rather
+than forwards.  Backward searches leave point at the beginning of the
+match.
 @end deffn
 
 @deffn Command word-search-forward string &optional limit noerror repeat
@@ -362,7 +364,7 @@ the two brackets are what this character alternative can match.
 
 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}
+(including the empty string).  It follows that @samp{c[ad]*r}
 matches @samp{cr}, @samp{car}, @samp{cdr}, @samp{caddaar}, etc.
 
 You can also include character ranges in a character alternative, by
@@ -385,35 +387,17 @@ matches both @samp{]} and @samp{-}.
 To include @samp{^} in a character alternative, put it anywhere but at
 the beginning.
 
-The beginning and end of a range of multibyte characters must be in
-the same character set (@pxref{Character Sets}).  Thus,
-@code{"[\x8e0-\x97c]"} is invalid because character 0x8e0 (@samp{a}
-with grave accent) is in the Emacs character set for Latin-1 but the
-character 0x97c (@samp{u} with diaeresis) is in the Emacs character
-set for Latin-2.  (We use Lisp string syntax to write that example,
-and a few others in the next few paragraphs, in order to include hex
-escape sequences in them.)
-
 If a range starts with a unibyte character @var{c} and ends with a
 multibyte character @var{c2}, the range is divided into two parts: one
 is @samp{@var{c}..?\377}, the other is @samp{@var{c1}..@var{c2}}, where
 @var{c1} is the first character of the charset to which @var{c2}
 belongs.
 
-You cannot always match all non-@acronym{ASCII} characters with the regular
-expression @code{"[\200-\377]"}.  This works when searching a unibyte
-buffer or string (@pxref{Text Representations}), but not in a multibyte
-buffer or string, because many non-@acronym{ASCII} characters have codes
-above octal 0377.  However, the regular expression @code{"[^\000-\177]"}
-does match all non-@acronym{ASCII} characters (see below regarding @samp{^}),
-in both multibyte and unibyte representations, because only the
-@acronym{ASCII} characters are excluded.
-
-A character alternative can also specify named
-character classes (@pxref{Char Classes}).  This is a POSIX feature whose
-syntax is @samp{[:@var{class}:]}.  Using a character class is equivalent
-to mentioning each of the characters in that class; but the latter is
-not feasible in practice, since some classes include thousands of
+A character alternative can also specify named character classes
+(@pxref{Char Classes}).  This is a POSIX feature whose syntax is
+@samp{[:@var{class}:]}.  Using a character class is equivalent to
+mentioning each of the characters in that class; but the latter is not
+feasible in practice, since some classes include thousands of
 different characters.
 
 @item @samp{[^ @dots{} ]}
@@ -431,6 +415,10 @@ A complemented character alternative can match a newline, unless newline is
 mentioned as one of the characters not to match.  This is in contrast to
 the handling of regexps in programs such as @code{grep}.
 
+You can specify named character classes, just like in character
+alternatives.  For instance, @samp{[^[:ascii:]]} matches any
+non-@acronym{ASCII} character.  @xref{Char Classes}.
+
 @item @samp{^}
 @cindex beginning of line in regexp
 When matching a buffer, @samp{^} matches the empty string, but only at the
@@ -571,6 +559,7 @@ through @samp{f} and @samp{A} through @samp{F}.
 
 @node Regexp Backslash
 @subsubsection Backslash Constructs in Regular Expressions
+@cindex backslash in regular expressions
 
   For the most part, @samp{\} followed by any character matches only
 that character.  However, there are several exceptions: certain
@@ -614,8 +603,8 @@ maximum.
 For example, @samp{c[ad]\@{1,2\@}r} matches the strings @samp{car},
 @samp{cdr}, @samp{caar}, @samp{cadr}, @samp{cdar}, and @samp{cddr}, and
 nothing else.@*
-@samp{\@{0,1\@}} or @samp{\@{,1\@}} is equivalent to @samp{?}. @*
-@samp{\@{0,\@}} or @samp{\@{,\@}} is equivalent to @samp{*}.   @*
+@samp{\@{0,1\@}} or @samp{\@{,1\@}} is equivalent to @samp{?}.@*
+@samp{\@{0,\@}} or @samp{\@{,\@}} is equivalent to @samp{*}.@*
 @samp{\@{1,\@}} is equivalent to @samp{+}.
 
 @item \( @dots{} \)
@@ -731,11 +720,15 @@ the characters that stand for them.
 @cindex @samp{\S} in regexp
 matches any character whose syntax is not @var{code}.
 
+@cindex category, regexp search for
 @item \c@var{c}
 matches any character whose category is @var{c}.  Here @var{c} is a
 character that represents a category: thus, @samp{c} for Chinese
 characters or @samp{g} for Greek characters in the standard category
-table.
+table.  You can see the list of all the currently defined categories
+with @kbd{M-x describe-categories @key{RET}}.  You can also define
+your own categories in addition to the standard ones using the
+@code{define-category} function (@pxref{Categories}).
 
 @item \C@var{c}
 matches any character whose category is not @var{c}.
@@ -922,7 +915,11 @@ for Font Lock mode.
 If the optional argument @var{paren} is non-@code{nil}, then the
 returned regular expression is always enclosed by at least one
 parentheses-grouping construct.  If @var{paren} is @code{words}, then
-that construct is additionally surrounded by @samp{\<} and @samp{\>}.
+that construct is additionally surrounded by @samp{\<} and @samp{\>};
+alternatively, if @var{paren} is @code{symbols}, then that construct
+is additionally surrounded by @samp{\_<} and @samp{\_>}
+(@code{symbols} is often appropriate when matching
+programming-language keywords and the like).
 
 This simplified definition of @code{regexp-opt} produces a
 regular expression which is equivalent to the actual value
@@ -1217,10 +1214,16 @@ search you wish to refer back to and the use of the match data.  If you
 can't avoid another intervening search, you must save and restore the
 match data around it, to prevent it from being overwritten.
 
+  Notice that all functions are allowed to overwrite the match data
+unless they're explicitly documented not to do so.  A consequence is
+that functions that are run implicitly in the background
+(@pxref{Timers}, and @ref{Idle Timers}) should likely save and restore
+the match data explicitly.
+
 @menu
-* Replacing Match::      Replacing a substring that was matched.
+* Replacing Match::       Replacing a substring that was matched.
 * Simple Match Data::     Accessing single items of match data,
-                           such as where a particular subexpression started.
+                            such as where a particular subexpression started.
 * Entire Match Data::     Accessing the entire match data at once, as a list.
 * Saving Match Data::     Saving and restoring the match data.
 @end menu
@@ -1831,7 +1834,3 @@ values of the variables @code{sentence-end-double-space}
 @code{sentence-end-without-period} and
 @code{sentence-end-without-space}.
 @end defun
-
-@ignore
-   arch-tag: c2573ca2-18aa-4839-93b8-924043ef831f
-@end ignore