From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: LanX Newsgroups: gmane.emacs.help Subject: Re: Transposing Regular Expression Date: Mon, 16 Nov 2009 18:13:11 -0800 (PST) Organization: http://groups.google.com Message-ID: <0d7b2ee7-0baa-40c2-a886-a09f24703585@d5g2000yqm.googlegroups.com> References: <0c6047e0-9b7c-482d-a083-3b24c9dd0a00@k13g2000prh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1258425675 26199 80.91.229.12 (17 Nov 2009 02:41:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 17 Nov 2009 02:41:15 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Nov 17 03:41:08 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NAE09-0004KC-K7 for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Nov 2009 03:41:05 +0100 Original-Received: from localhost ([127.0.0.1]:52426 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NAE08-0008Vd-QK for geh-help-gnu-emacs@m.gmane.org; Mon, 16 Nov 2009 21:41:04 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!postnews.google.com!d5g2000yqm.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 61 Original-NNTP-Posting-Host: 94.219.215.65 Original-X-Trace: posting.google.com 1258423991 26792 127.0.0.1 (17 Nov 2009 02:13:11 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 17 Nov 2009 02:13:11 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: d5g2000yqm.googlegroups.com; posting-host=94.219.215.65; posting-account=W9fpQwoAAADZYmkl-8sXk1VPxG3rq-Pd User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.0.14) Gecko/2009090216 Ubuntu/8.10 (intrepid) Firefox/3.0.14, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:174732 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:69806 Archived-At: > That's not exactly what I'm looking for as I really need 2 words/ > characters to be swapped. =A0For instance, swapping "true" and "false" > in a region. OK let's talk perl to understand ... Thats what you want? perl -e ' $_=3D"EXAMPLE true EXAMPLE false EXAMPLE"; %trans=3D(true=3D>"false"); %trans=3D(%trans,reverse %trans); $pattern=3Djoin "|",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=3D>2, 2=3D>3, 3=3D>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=3D(true=3D>"false"); %trans=3D(%trans,reverse %trans); $pattern=3Djoin "|",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=3D(true=3D>\"false\"); %trans=3D(%trans,reverse %trans); $pattern=3Djoin \"|\",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