[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Transposing Regular Expression

From: harven
Subject: Re: Transposing Regular Expression
Date: Wed, 11 Nov 2009 21:51:42 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin)

jrwats <address@hidden> writes:

> 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 \#) ?

The emacs manual gives the following snippet for swapping two values.

     M-x replace-regexp <RET> \(x\)\|y <RET>
     \,(if \1 "y" "x") <RET>

Note that you can swap two words by the same method. This is not exactly
what you are looking for though. 

There are many functions that have been devised to swap values
according to an alist, e.g. for converting accents from iso8859-1 to html.
These can be easily adapted to get a command emulating tr.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]