Mercurial > emacs
comparison man/search.texi @ 56251:651f09f1cac0
(Regexp Replace): Rewrite description of \# \, and \?.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 25 Jun 2004 21:27:55 +0000 |
parents | 09f1db905d2e |
children | b1ae5333057a |
comparison
equal
deleted
inserted
replaced
56250:b7af6a88f099 | 56251:651f09f1cac0 |
---|---|
991 | 991 |
992 The @kbd{M-x replace-string} command replaces exact matches for a | 992 The @kbd{M-x replace-string} command replaces exact matches for a |
993 single string. The similar command @kbd{M-x replace-regexp} replaces | 993 single string. The similar command @kbd{M-x replace-regexp} replaces |
994 any match for a specified pattern. | 994 any match for a specified pattern. |
995 | 995 |
996 In @code{replace-regexp}, the @var{newstring} need not be constant: it | 996 In @code{replace-regexp}, the @var{newstring} need not be constant: |
997 can refer to all or part of what is matched by the @var{regexp}. | 997 it can refer to all or part of what is matched by the @var{regexp}. |
998 @samp{\&} in @var{newstring} stands for the entire match being replaced. | 998 @samp{\&} in @var{newstring} stands for the entire match being |
999 @samp{\@var{d}} in @var{newstring}, where @var{d} is a digit, stands for | 999 replaced. @samp{\@var{d}} in @var{newstring}, where @var{d} is a |
1000 whatever matched the @var{d}th parenthesized grouping in @var{regexp}. | 1000 digit, stands for whatever matched the @var{d}th parenthesized |
1001 To include a @samp{\} in the text to replace with, you must enter | 1001 grouping in @var{regexp}. @samp{\#} refers to the count of |
1002 @samp{\\}. For example, | 1002 replacements already made in this command, as a decimal number. In |
1003 the first replacement, @samp{\#} stands for @samp{0}; in the second, | |
1004 for @samp{1}; and so on. For example, | |
1003 | 1005 |
1004 @example | 1006 @example |
1005 M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET} | 1007 M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET} |
1006 @end example | 1008 @end example |
1007 | 1009 |
1012 @example | 1014 @example |
1013 M-x replace-regexp @key{RET} \(c[ad]+r\)-safe @key{RET} \1 @key{RET} | 1015 M-x replace-regexp @key{RET} \(c[ad]+r\)-safe @key{RET} \1 @key{RET} |
1014 @end example | 1016 @end example |
1015 | 1017 |
1016 @noindent | 1018 @noindent |
1017 performs the inverse transformation. | 1019 performs the inverse transformation. To include a @samp{\} in the |
1018 | 1020 text to replace with, you must enter @samp{\\}. |
1019 You can also use arbitrary Lisp expressions evaluated at replacement | 1021 |
1020 time by placing @samp{\,} before them in the replacement string. Inside | 1022 You can also use Lisp expressions to calculate parts of the |
1021 of those expressions, the symbols @samp{\&} and @samp{\@var{n}} refer to | 1023 replacement string. To do this, write @samp{\,} followed by the |
1022 match and submatch strings like described above (though @var{n} may | 1024 expression in the replacement string. Each replacement calculates the |
1023 exceed 9 here, and you get @code{nil} if nothing matches). @samp{\#&} | 1025 value of the expression, which ought to be a string, and uses it in |
1024 and @samp{\#@var{n}} refer to those strings converted to numbers. | 1026 the replacement string in place of the expression itself. If the |
1025 @samp{\#} is short for @samp{replace-count}, the number of already | 1027 expression is a symbol, one space in the replacement string after the |
1026 completed replacements. This particular shorthand can also be used | 1028 symbol name counts as part of the symbol name, so the value replaces |
1027 outside of @samp{\,}. | 1029 them both. |
1030 | |
1031 Inside such an expression, @samp{\&} and @samp{\@var{n}} used as | |
1032 subexpressions refer respectively to the entire match as a string, and | |
1033 to a submatch as a string. @var{n} may exceed 9 here, and the value | |
1034 of @samp{\@var{n}} is @code{nil} if subexpression @var{n} did not | |
1035 match. You can also use @samp{\#&} and @samp{\#@var{n}} refer to | |
1036 those matches converted to numbers (this is valid when the match or | |
1037 submatch has the form of a number). @samp{\#} stands for the number | |
1038 of already-completed replacements. | |
1028 | 1039 |
1029 Repeating our example to exchange @samp{x} and @samp{y}, we can thus | 1040 Repeating our example to exchange @samp{x} and @samp{y}, we can thus |
1030 do it also this way: | 1041 do it also this way: |
1031 | 1042 |
1032 @example | 1043 @example |
1033 M-x replace-regexp @key{RET} \(x\)\|y @key{RET} | 1044 M-x replace-regexp @key{RET} \(x\)\|y @key{RET} |
1034 \,(if \1 "y" "x") @key{RET} | 1045 \,(if \1 "y" "x") @key{RET} |
1035 @end example | 1046 @end example |
1036 | 1047 |
1037 One function that comes handy in Lisp replacements is @samp{format} | 1048 The @code{format} function (@pxref{Formatting Strings,,,elisp, GNU |
1038 (@pxref{Formatting Strings,,,elisp, GNU Emacs Lisp Reference Manual}). | 1049 Emacs Lisp Reference Manual}) comes in handy for computing replacement |
1039 For example, to add consecutively numbered strings like @samp{ABC00042} | 1050 strings for @samp{\,}. For example, to add consecutively numbered |
1040 to columns 73 @w{to 80} (unless they are already occupied), you can use | 1051 strings like @samp{ABC00042} to columns 73 @w{to 80} (unless they are |
1052 already occupied), you can use | |
1041 | 1053 |
1042 @example | 1054 @example |
1043 M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET} | 1055 M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET} |
1044 \,(format "%-72sABC%05d" \& \#) @key{RET} | 1056 \,(format "%-72sABC%05d" \& \#) @key{RET} |
1045 @end example | 1057 @end example |
1046 | 1058 |
1047 Another feature you can use in the replacement string of Regexp | 1059 If you want to enter part of the replacement string by hand each |
1048 commands is @samp{\?}. In that case you will be allowed to edit the | 1060 time, use @samp{\?} in the replacement string. Each replacement will |
1049 replacement string at the given position before the replacement gets | 1061 enter a recursive edit, with point at the position where the @samp{\?} |
1050 performed. Lisp style replacements are performed before @samp{\?} gets | 1062 was. For example, |
1051 executed. For example, | |
1052 | 1063 |
1053 @example | 1064 @example |
1054 M-x replace-regexp @key{RET} \footnote@{ @key{RET} | 1065 M-x replace-regexp @key{RET} \footnote@{ @key{RET} |
1055 \&\\label@{fn:\#\?@} @key{RET} | 1066 \&\\label@{fn:\#\?@} @key{RET} |
1056 @end example | 1067 @end example |
1057 | 1068 |
1058 @noindent | 1069 @noindent |
1059 will add labels starting with @samp{\label@{fn:0@}} to occurences of | 1070 will add labels starting with @samp{\label@{fn:0@}} to occurences of |
1060 @samp{\footnote@{}, but letting you edit each replacement before | 1071 @samp{\footnote@{}, but letting you edit each replacement before |
1061 performing it. If you want labels starting at 1, use @samp{\,(1+ \#)} | 1072 performing it. To number the labels starting at 1, use @samp{\,(1+ |
1062 instead of @samp{\#}. | 1073 \#)} instead of @samp{\#}. |
1063 | 1074 |
1064 @node Replacement and Case, Query Replace, Regexp Replace, Replace | 1075 @node Replacement and Case, Query Replace, Regexp Replace, Replace |
1065 @subsection Replace Commands and Case | 1076 @subsection Replace Commands and Case |
1066 | 1077 |
1067 If the first argument of a replace command is all lower case, the | 1078 If the first argument of a replace command is all lower case, the |