[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Transposing Regular Expression
From: |
Andreas Röhler |
Subject: |
Re: Transposing Regular Expression |
Date: |
Mon, 16 Nov 2009 13:16:35 +0100 |
User-agent: |
Thunderbird 2.0.0.19 (X11/20081227) |
jrwats wrote:
> Perl provides the transpose operator:
> =~ tr/abc/xyz/ not really a regular expression, but exchanges 'x' for
> 'a', 'y' for 'b', and 'z' for 'c' in the source string.
>
> My question is how to accomplish this in emacs. When only needing to
> tranpose 2 characters that need to replace each other, (the equivalent
> perl expression woud be =~ tr/ab/ba/ as an example, I could simply
> regexp replace 'a' with a unique letter or symbol, maybe '$' for
> instance, then replace all b's with a's and all $'s with b's. This
> obviously becomes unweildy after we start transposing more than 2
> characters. My question is, now that emacs provides fancy regexp
> replace clauses: \# for the number match, and arbitrary lisp
> expressions \,(some-lisp), etc, is there a way to accomplish this in
> one fell swoop via a very crazy regular expression find-replace? Also
> is there a list of meaningful regular expression search escape
> characters somewhere (like \#) ?
>
IIUC you are going to change strings.
That's a simple task then with no need to employ
\# for the number match ore other advanced features.
Interactivly just call
M-x query-replace - putting in your strings at the prompt.
>From a program use for example
(while
(search-forward "abc" nil t 1)
(replace-match "xyz"))
Andreas
--
https://code.launchpad.net/s-x-emacs-werkstatt/
http://bazaar.launchpad.net/~a-roehler/python-mode/python-mode.el/