181 |
point, just as a forward search finds matches that begin after it. |
point, just as a forward search finds matches that begin after it. |
182 |
|
|
183 |
The characters @kbd{C-y} and @kbd{C-w} can be used in incremental |
The characters @kbd{C-y} and @kbd{C-w} can be used in incremental |
184 |
search to grab text from the buffer into the search string. This makes |
search to grab text from the buffer into the search string. This |
185 |
it convenient to search for another occurrence of text at point. |
makes it convenient to search for another occurrence of text at point. |
186 |
@kbd{C-w} copies the word after point as part of the search string, |
@kbd{C-w} copies the character or word after point as part of the |
187 |
advancing point over that word. Another @kbd{C-s} to repeat the search |
search string, advancing point over it. (The decision, whether to |
188 |
will then search for a string including that word. @kbd{C-y} is similar |
copy a character or a word, is heuristic.) Another @kbd{C-s} to |
189 |
to @kbd{C-w} but copies all the rest of the current line into the search |
repeat the search will then search for a string including that |
190 |
string. Both @kbd{C-y} and @kbd{C-w} convert the text they copy to |
character or word. |
191 |
lower case if the search is currently not case-sensitive; this is so the |
|
192 |
search remains case-insensitive. |
@kbd{C-y} is similar to @kbd{C-w} but copies all the rest of the |
193 |
|
current line into the search string. Both @kbd{C-y} and @kbd{C-w} |
194 |
|
convert the text they copy to lower case if the search is currently |
195 |
|
not case-sensitive; this is so the search remains case-insensitive. |
196 |
|
|
197 |
The character @kbd{M-y} copies text from the kill ring into the search |
The character @kbd{M-y} copies text from the kill ring into the search |
198 |
string. It uses the same text that @kbd{C-y} as a command would yank. |
string. It uses the same text that @kbd{C-y} as a command would yank. |
413 |
the string @samp{fo}. Still trivial. To do something nontrivial, you |
the string @samp{fo}. Still trivial. To do something nontrivial, you |
414 |
need to use one of the special characters. Here is a list of them. |
need to use one of the special characters. Here is a list of them. |
415 |
|
|
416 |
@table @kbd |
@table @asis |
417 |
@item .@: @r{(Period)} |
@item @kbd{.}@: @r{(Period)} |
418 |
is a special character that matches any single character except a newline. |
is a special character that matches any single character except a newline. |
419 |
Using concatenation, we can make regular expressions like @samp{a.b}, which |
Using concatenation, we can make regular expressions like @samp{a.b}, which |
420 |
matches any three-character string that begins with @samp{a} and ends with |
matches any three-character string that begins with @samp{a} and ends with |
421 |
@samp{b}.@refill |
@samp{b}.@refill |
422 |
|
|
423 |
@item * |
@item @kbd{*} |
424 |
is not a construct by itself; it is a postfix operator that means to |
is not a construct by itself; it is a postfix operator that means to |
425 |
match the preceding regular expression repetitively as many times as |
match the preceding regular expression repetitively as many times as |
426 |
possible. Thus, @samp{o*} matches any number of @samp{o}s (including no |
possible. Thus, @samp{o*} matches any number of @samp{o}s (including no |
441 |
The next alternative is for @samp{a*} to match only two @samp{a}s. |
The next alternative is for @samp{a*} to match only two @samp{a}s. |
442 |
With this choice, the rest of the regexp matches successfully.@refill |
With this choice, the rest of the regexp matches successfully.@refill |
443 |
|
|
444 |
@item + |
@item @kbd{+} |
445 |
is a postfix operator, similar to @samp{*} except that it must match |
is a postfix operator, similar to @samp{*} except that it must match |
446 |
the preceding expression at least once. So, for example, @samp{ca+r} |
the preceding expression at least once. So, for example, @samp{ca+r} |
447 |
matches the strings @samp{car} and @samp{caaaar} but not the string |
matches the strings @samp{car} and @samp{caaaar} but not the string |
448 |
@samp{cr}, whereas @samp{ca*r} matches all three strings. |
@samp{cr}, whereas @samp{ca*r} matches all three strings. |
449 |
|
|
450 |
@item ? |
@item @kbd{?} |
451 |
is a postfix operator, similar to @samp{*} except that it can match the |
is a postfix operator, similar to @samp{*} except that it can match the |
452 |
preceding expression either once or not at all. For example, |
preceding expression either once or not at all. For example, |
453 |
@samp{ca?r} matches @samp{car} or @samp{cr}; nothing else. |
@samp{ca?r} matches @samp{car} or @samp{cr}; nothing else. |
454 |
|
|
455 |
@item *?, +?, ?? |
@item @kbd{*?}, @kbd{+?}, @kbd{??} |
456 |
@cindex non-greedy regexp matching |
@cindex non-greedy regexp matching |
457 |
are non-greedy variants of the operators above. The normal operators |
are non-greedy variants of the operators above. The normal operators |
458 |
@samp{*}, @samp{+}, @samp{?} are @dfn{greedy} in that they match as |
@samp{*}, @samp{+}, @samp{?} are @dfn{greedy} in that they match as |
473 |
a newline, it matches the whole string. Since it @emph{can} match |
a newline, it matches the whole string. Since it @emph{can} match |
474 |
starting at the first @samp{a}, it does. |
starting at the first @samp{a}, it does. |
475 |
|
|
476 |
@item \@{@var{n}\@} |
@item @kbd{\@{@var{n}\@}} |
477 |
is a postfix operator that specifies repetition @var{n} times---that |
is a postfix operator that specifies repetition @var{n} times---that |
478 |
is, the preceding regular expression must match exactly @var{n} times |
is, the preceding regular expression must match exactly @var{n} times |
479 |
in a row. For example, @samp{x\@{4\@}} matches the string @samp{xxxx} |
in a row. For example, @samp{x\@{4\@}} matches the string @samp{xxxx} |
480 |
and nothing else. |
and nothing else. |
481 |
|
|
482 |
@item \@{@var{n},@var{m}\@} |
@item @kbd{\@{@var{n},@var{m}\@}} |
483 |
is a postfix operator that specifies repetition between @var{n} and |
is a postfix operator that specifies repetition between @var{n} and |
484 |
@var{m} times---that is, the preceding regular expression must match |
@var{m} times---that is, the preceding regular expression must match |
485 |
at least @var{n} times, but no more than @var{m} times. If @var{m} is |
at least @var{n} times, but no more than @var{m} times. If @var{m} is |
488 |
equivalent to @samp{?}. @* @samp{\@{0,\@}} is equivalent to |
equivalent to @samp{?}. @* @samp{\@{0,\@}} is equivalent to |
489 |
@samp{*}. @* @samp{\@{1,\@}} is equivalent to @samp{+}. |
@samp{*}. @* @samp{\@{1,\@}} is equivalent to @samp{+}. |
490 |
|
|
491 |
@item [ @dots{} ] |
@item @kbd{[ @dots{} ]} |
492 |
is a @dfn{character set}, which begins with @samp{[} and is terminated |
is a @dfn{character set}, which begins with @samp{[} and is terminated |
493 |
by @samp{]}. In the simplest case, the characters between the two |
by @samp{]}. In the simplest case, the characters between the two |
494 |
brackets are what this set can match. |
brackets are what this set can match. |
523 |
be non-letters. The behavior of a mixed-case range such as @samp{A-z} |
be non-letters. The behavior of a mixed-case range such as @samp{A-z} |
524 |
is somewhat ill-defined, and it may change in future Emacs versions. |
is somewhat ill-defined, and it may change in future Emacs versions. |
525 |
|
|
526 |
@item [^ @dots{} ] |
@item @kbd{[^ @dots{} ]} |
527 |
@samp{[^} begins a @dfn{complemented character set}, which matches any |
@samp{[^} begins a @dfn{complemented character set}, which matches any |
528 |
character except the ones specified. Thus, @samp{[^a-z0-9A-Z]} matches |
character except the ones specified. Thus, @samp{[^a-z0-9A-Z]} matches |
529 |
all characters @emph{except} ASCII letters and digits. |
all characters @emph{except} ASCII letters and digits. |
536 |
mentioned as one of the characters not to match. This is in contrast to |
mentioned as one of the characters not to match. This is in contrast to |
537 |
the handling of regexps in programs such as @code{grep}. |
the handling of regexps in programs such as @code{grep}. |
538 |
|
|
539 |
@item ^ |
@item @kbd{^} |
540 |
is a special character that matches the empty string, but only at the |
is a special character that matches the empty string, but only at the |
541 |
beginning of a line in the text being matched. Otherwise it fails to |
beginning of a line in the text being matched. Otherwise it fails to |
542 |
match anything. Thus, @samp{^foo} matches a @samp{foo} that occurs at |
match anything. Thus, @samp{^foo} matches a @samp{foo} that occurs at |
543 |
the beginning of a line. |
the beginning of a line. |
544 |
|
|
545 |
@item $ |
@item @kbd{$} |
546 |
is similar to @samp{^} but matches only at the end of a line. Thus, |
is similar to @samp{^} but matches only at the end of a line. Thus, |
547 |
@samp{x+$} 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. |
548 |
|
|
549 |
@item \ |
@item @kbd{\} |
550 |
has two functions: it quotes the special characters (including |
has two functions: it quotes the special characters (including |
551 |
@samp{\}), and it introduces additional special constructs. |
@samp{\}), and it introduces additional special constructs. |
552 |
|
|
801 |
of the pattern and asks you whether to replace it. |
of the pattern and asks you whether to replace it. |
802 |
|
|
803 |
The replace commands normally operate on the text from point to the |
The replace commands normally operate on the text from point to the |
804 |
end of the buffer; however, in Transient Mark mode, when the mark is |
end of the buffer; however, in Transient Mark mode (@pxref{Transient |
805 |
active, they operate on the region. The replace commands all replace |
Mark}), when the mark is active, they operate on the region. The |
806 |
one string (or regexp) with one replacement string. It is possible to |
replace commands all replace one string (or regexp) with one |
807 |
perform several replacements in parallel using the command |
replacement string. It is possible to perform several replacements in |
808 |
@code{expand-region-abbrevs} (@pxref{Expanding Abbrevs}). |
parallel using the command @code{expand-region-abbrevs} |
809 |
|
(@pxref{Expanding Abbrevs}). |
810 |
|
|
811 |
@menu |
@menu |
812 |
* Unconditional Replace:: Replacing all matches for a string. |
* Unconditional Replace:: Replacing all matches for a string. |
845 |
A numeric argument restricts replacement to matches that are surrounded |
A numeric argument restricts replacement to matches that are surrounded |
846 |
by word boundaries. The argument's value doesn't matter. |
by word boundaries. The argument's value doesn't matter. |
847 |
|
|
848 |
|
What if you want to exchange @samp{x} and @samp{y}: replace every @samp{x} with a @samp{y} and vice versa? You can do it this way: |
849 |
|
|
850 |
|
@example |
851 |
|
M-x query-replace @key{RET} x @key{RET} @@TEMP@@ @key{RET} |
852 |
|
M-x query-replace @key{RET} y @key{RET} x @key{RET} |
853 |
|
M-x query-replace @key{RET} @@TEMP@@ @key{RET} y @key{RET} |
854 |
|
@end example |
855 |
|
|
856 |
|
@noindent |
857 |
|
This works provided the string @samp{@@TEMP@@} does not appear |
858 |
|
in your text. |
859 |
|
|
860 |
@node Regexp Replace, Replacement and Case, Unconditional Replace, Replace |
@node Regexp Replace, Replacement and Case, Unconditional Replace, Replace |
861 |
@subsection Regexp Replacement |
@subsection Regexp Replacement |
862 |
|
|
1074 |
after each matching line. |
after each matching line. |
1075 |
|
|
1076 |
@kindex RET @r{(Occur mode)} |
@kindex RET @r{(Occur mode)} |
1077 |
|
@kindex o @r{(Occur mode)} |
1078 |
|
@kindex C-o @r{(Occur mode)} |
1079 |
The buffer @samp{*Occur*} containing the output serves as a menu for |
The buffer @samp{*Occur*} containing the output serves as a menu for |
1080 |
finding the occurrences in their original context. Click @kbd{Mouse-2} |
finding the occurrences in their original context. Click |
1081 |
on an occurrence listed in @samp{*Occur*}, or position point there and |
@kbd{Mouse-2} on an occurrence listed in @samp{*Occur*}, or position |
1082 |
type @key{RET}; this switches to the buffer that was searched and |
point there and type @key{RET}; this switches to the buffer that was |
1083 |
moves point to the original of the chosen occurrence. |
searched and moves point to the original of the chosen occurrence. |
1084 |
|
@kbd{o} and @kbd{C-o} display the match in another window; @kbd{C-o} |
1085 |
|
does not select it. |
1086 |
|
|
1087 |
@item M-x list-matching-lines |
@item M-x list-matching-lines |
1088 |
Synonym for @kbd{M-x occur}. |
Synonym for @kbd{M-x occur}. |