all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: LanX <lanx.perl@googlemail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Transposing Regular Expression
Date: Mon, 16 Nov 2009 18:13:11 -0800 (PST)	[thread overview]
Message-ID: <0d7b2ee7-0baa-40c2-a886-a09f24703585@d5g2000yqm.googlegroups.com> (raw)
In-Reply-To: 0c6047e0-9b7c-482d-a083-3b24c9dd0a00@k13g2000prh.googlegroups.com

> That's not exactly what I'm looking for as I really need 2 words/
> characters to be swapped.  For instance, swapping "true" and "false"
> in a region.

OK let's talk perl to understand ... Thats what you want?

perl -e '
$_="EXAMPLE true EXAMPLE false EXAMPLE";
%trans=(true=>"false");
%trans=(%trans,reverse %trans);
$pattern=join "|",keys %trans;
s/($pattern)/$trans{$1}/g;
print "OUTPUT: $_";
'

OUTPUT: EXAMPLE false EXAMPLE true EXAMPLE

(please note: if you don't just want a dual swap but a more
complicated permutation, skip the "reverse" part and provide %trans as
needed like e.g. (1=>2, 2=>3, 3=>1))

You can easily adjust the lisp code I gave you to do that, you need to
change the lambda to do a hash look up and call it within one of the
region-replace functions!

Though for me the corresponding lisp code for hashes seem a little
lengthy...

(I now there are alists, but I leave this solution to others)

So why don't you just pipe a region thru the perl script above?

Typing
C-u M-|
perl -pe '
%trans=(true=>"false");
%trans=(%trans,reverse %trans);
$pattern=join "|",keys %trans;
s/($pattern)/$trans{$1}/g;
'

Can be easily adjusted to do what you want.

And if you type C-x ESC ESC you get the corresponding lisp wrapper for
free
---------
(shell-command-on-region (region-beginning) (region-end) "perl -pe '
%trans=(true=>\"false\");
%trans=(%trans,reverse %trans);
$pattern=join \"|\",keys %trans;
s/($pattern)/$trans{$1}/g;
'
" (quote -) (quote -) nil t)
--------


Couldn't be easier... and the call overhead to perl is really not
observable...

HTH
  LanX


      reply	other threads:[~2009-11-17  2:13 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
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 [this message]

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

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

  git send-email \
    --in-reply-to=0d7b2ee7-0baa-40c2-a886-a09f24703585@d5g2000yqm.googlegroups.com \
    --to=lanx.perl@googlemail.com \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.