diff options
Diffstat (limited to 'doc/lispref/searching.texi')
-rw-r--r-- | doc/lispref/searching.texi | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/doc/lispref/searching.texi b/doc/lispref/searching.texi index 16a8e56e90a..4d5ae3cb437 100644 --- a/doc/lispref/searching.texi +++ b/doc/lispref/searching.texi @@ -251,6 +251,11 @@ matches in the target buffer are highlighted. Each parenthesized sub-expression of the regexp is shown in a distinct face, which makes it easier to verify even very complex regexps. + Note that by default Emacs search ignores case (@pxref{Searching and +Case}). To enable case-sensitive regexp search and match, bind +@code{case-fold-search} to @code{nil} around the code you want to be +case-sensitive. + @menu * Syntax of Regexps:: Rules for writing regular expressions. * Regexp Example:: Illustrates regular expression syntax. @@ -363,7 +368,7 @@ preceding expression either once or not at all. For example, @anchor{Non-greedy repetition} @item @samp{*?}, @samp{+?}, @samp{??} @cindex non-greedy repetition characters in regexp -These are @dfn{non-greedy} variants of the operators @samp{*}, @samp{+} +are @dfn{non-greedy} variants of the operators @samp{*}, @samp{+} and @samp{?}. Where those operators match the largest possible substring (consistent with matching the entire containing expression), the non-greedy variants match the smallest possible substring @@ -438,6 +443,13 @@ including newline. However, a reversed range should always be from the letter @samp{z} to the letter @samp{a} to make it clear that it is not a typo; for example, @samp{[+-*/]} should be avoided, because it matches only @samp{/} rather than the likely-intended four characters. + +@item +If the end points of a range are raw 8-bit bytes (@pxref{Text +Representations}), or if the range start is ASCII and the end is a raw +byte (as in @samp{[a-\377]}), the range will match only ASCII +characters and raw 8-bit bytes, but not non-ASCII characters. This +feature is intended for searching text in unibyte buffers and strings. @end enumerate Some kinds of character alternatives are not the best style even @@ -2528,9 +2540,9 @@ associated with it still exists. @cindex replacement after search @cindex searching and replacing - If you want to find all matches for a regexp in part of the buffer, -and replace them, the best way is to write an explicit loop using -@code{re-search-forward} and @code{replace-match}, like this: + If you want to find all matches for a regexp in part of the buffer +and replace them, the most flexible way is to write an explicit loop +using @code{re-search-forward} and @code{replace-match}, like this: @example (while (re-search-forward "foo[ \t]+bar" nil t) @@ -2541,9 +2553,33 @@ and replace them, the best way is to write an explicit loop using @xref{Replacing Match,, Replacing the Text that Matched}, for a description of @code{replace-match}. - However, replacing matches in a string is more complex, especially -if you want to do it efficiently. So Emacs provides two functions to do -this. + It may be more convenient to limit the replacements to a specific +region. The function @code{replace-regexp-in-region} does that. + +@defun replace-regexp-in-region regexp replacement &optional start end +This function replaces all the occurrences of @var{regexp} with +@var{replacement} in the region of buffer text between @var{start} and +@var{end}; @var{start} defaults to position of point, and @var{end} +defaults to the last accessible position of the buffer. The search +for @var{regexp} is case-sensitive, and @var{replacement} is inserted +without changing its letter-case. The @var{replacement} string can +use the same special elements starting with @samp{\} as +@code{replace-match} does. The function returns the number of +replaced occurrences, or @code{nil} if @var{regexp} is not found. The +function preserves the position of point. + +@example +(replace-regexp-in-region "foo[ \t]+bar" "foobar") +@end example +@end defun + +@defun replace-string-in-region string replacement &optional start end + This function works similarly to @code{replace-regexp-in-region}, +but searches for, and replaces, literal @var{string}s instead of +regular expressions. +@end defun + + Emacs also has special functions for replacing matches in a string. @defun replace-regexp-in-string regexp rep string &optional fixedcase literal subexp start This function copies @var{string} and searches it for matches for |