[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [h-e-w] RegExp in
From: |
Dr Francis J. Wright |
Subject: |
RE: [h-e-w] RegExp in |
Date: |
Wed, 15 Sep 2004 15:51:11 +0100 |
> From:
> address@hidden
> [mailto:address@hidden
> rg] On Behalf Of Slava
> Sent: Wednesday, 15 September 2004 12:22 pm
> To: address@hidden
> Subject: [h-e-w] RegExp in
>
> Next code doesn't search anything. Why?
>
> (defun my-function ()
> "Replace '%' with '\%' (it should skip percent signs already
> with the slash"
> (interactive)
> (while (re-search-forward "\([^\\]\)%" nil t)
> (replace-match "\1%" nil nil)))
>
> It works without '\(...\)', but then I loose one symbol
> before each percent sign being replaced.
One problem is that the read syntax for a \ in a string is "\\", hence you
need to double every \, not only the one in the []. However, with that fix
I think your code would perform an identity operation, i.e. effectively do
nothing. So I suggest something like this:
(while (re-search-forward "[^\\]%" nil t)
(backward-char)
(insert "\\"))
The search should position point after the %, provided % is not preceded by
\. Hence the (backward-char). Note that I haven't tested this code.
Francis