[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: query-replace-regexp-swap ?
From: |
rgb |
Subject: |
Re: query-replace-regexp-swap ? |
Date: |
25 Nov 2005 18:03:18 -0800 |
User-agent: |
G2/0.2 |
Bastien wrote:
> I often come across this problem: how to swap two strings in a buffer?
> For instance, say that you want to exchange #000 and #FFF in an HTML
> page: all #000 should become #FFF and all #FFF should become #000.
>
> How to do this with one *single* function ?
>
> For now i use this:
>
> ==%<==================================================================
> (defun query-replace-regexp-swap (regexp-a regexp-b beg end)...
Does this look like what you wanted?
(defun swap-text (str1 str2 beg end)
"Changes all STR1 to STR2 and all STR2 to STR1"
(interactive "sString A: \nsString B: \nr")
(goto-char beg)
(while (re-search-forward
(concat "\\(?:\\(" (regexp-quote str1) "\\)\\|\\("
(regexp-quote str2) "\\)\\)") end t)
(if (match-string 1)
(replace-match str2 t t)
(replace-match str1 t t))))