unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: harven <harven@free.fr>
To: help-gnu-emacs@gnu.org
Subject: Re: Transposing Regular Expression
Date: Wed, 11 Nov 2009 21:51:42 +0100	[thread overview]
Message-ID: <m2bpj8944h.fsf@free.fr> (raw)
In-Reply-To: b40df352-3ed3-4f35-9304-86372ecddf9c@x5g2000prf.googlegroups.com

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

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.

Cheers


  reply	other threads:[~2009-11-11 20:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-11 17:25 Transposing Regular Expression jrwats
2009-11-11 20:51 ` harven [this message]
2009-11-11 21:45 ` Andreas Politz
2009-11-12  6:40 ` LanX
2009-11-16 12:16 ` Andreas Röhler
     [not found] ` <mailman.10804.1258373854.2239.help-gnu-emacs@gnu.org>
2009-11-16 16:28   ` jrwats
2009-11-17  2:13     ` LanX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2bpj8944h.fsf@free.fr \
    --to=harven@free.fr \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).