[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Transposing Regular Expression
From: |
Andreas Politz |
Subject: |
Re: Transposing Regular Expression |
Date: |
Wed, 11 Nov 2009 22:45:17 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
jrwats <jrwats@gmail.com> 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 \#) ?
You can use literal backreferences (e.g. \1) in the replacement part of
interactive uses of replace-regexp. Combine that with expressions \,
and you could write a c-compiler in emacs-regexp/elisp.
The regular expression syntax is described in the elisp manual.
(info "(elisp)Regular Expressions")
-ap