From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jambunathan K Newsgroups: gmane.emacs.help Subject: Re: Query replace regex with 2 alternatives Date: Sat, 08 Dec 2012 01:41:25 +0530 Message-ID: <87txrx8vwy.fsf@gmail.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1354910925 25635 80.91.229.3 (7 Dec 2012 20:08:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 7 Dec 2012 20:08:45 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Dan Espen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Dec 07 21:08:58 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Th4EA-0005cG-VN for geh-help-gnu-emacs@m.gmane.org; Fri, 07 Dec 2012 21:08:55 +0100 Original-Received: from localhost ([::1]:60501 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Th4Dy-0007Xw-Ma for geh-help-gnu-emacs@m.gmane.org; Fri, 07 Dec 2012 15:08:42 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:49535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Th4Dp-0007Xo-7I for help-gnu-emacs@gnu.org; Fri, 07 Dec 2012 15:08:38 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Th4Dn-0004p1-VC for help-gnu-emacs@gnu.org; Fri, 07 Dec 2012 15:08:33 -0500 Original-Received: from mail-pb0-f41.google.com ([209.85.160.41]:63910) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Th4Dn-0004ov-PV for help-gnu-emacs@gnu.org; Fri, 07 Dec 2012 15:08:31 -0500 Original-Received: by mail-pb0-f41.google.com with SMTP id xa7so598228pbc.0 for ; Fri, 07 Dec 2012 12:08:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=yzSgigw5M/vrJmYPJ0qXj3z4kq38EqcTojLyYH7VJy0=; b=B7+R+WnuMwd93p0juBBVRsshgRCFpP3rhpEsunhb17SQk+aQWWFJBhZYB+uF8qg4rd IThK6xgaQ73F5AAoYJM8RV0X/kNPqKj1rEkESEaR+Q1tpDw43B0j7snxoN9KOMCTjARK g0urLbkZ7yPcMSwz+WruSEKrAS/f1NOfKX1t3HoYUKr/cBDrbgSMc36NBBYUz3vYOLNu BmO/l+83WN65WWUqVFlTN3oYWo+a09vgknFZvtrBuSW4092XsTtem233Ict1dVc/fHO6 2RZgo6d6oUnKgHYLbOMGuXcXu31m2gsPHLy1FgcoELeZv0aoAMC8im8xQUEQVXpLzBkq lvNw== Original-Received: by 10.69.1.71 with SMTP id be7mr18754824pbd.45.1354910910617; Fri, 07 Dec 2012 12:08:30 -0800 (PST) Original-Received: from debian-6.05 ([101.62.89.217]) by mx.google.com with ESMTPS id ip8sm7183049pbc.36.2012.12.07.12.08.27 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 07 Dec 2012 12:08:29 -0800 (PST) In-Reply-To: (Dan Espen's message of "Fri, 07 Dec 2012 11:51:44 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.160.41 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88075 Archived-At: Dan Espen writes: > Could use some help on query/replace/regex. > > I have an html file full of < and >. > I want to replace only some of the pairs with "[" and "]". > > I figured out the match string: > > "\\(<\\|>\\) > > (typed as) > > "\(<\|>\) > > but when it comes to the replacement, I'm not clear on how to say, > first match gets [ and second match gets ]. > I believe emacs can do it but I don't see it documented. > I see references to \1 \2 but not in the replace string. rx-to-string is the easiest way to build such an regexp. C-h f rx Do this 1. M-x ielm RET 2. Copy the below regexp to the prompt (rx-to-string '(and (group-n 1 "<") (group-n 2 (minimal-match (zero-or-more anything))) (group-n 3 ">"))) Here is a sample session. ,---- | ELISP> (rx-to-string '(and (group-n 1 "<") | (group-n 2 (minimal-match | (zero-or-more anything))) | (group-n 3 ">"))) | "\\(?:\\(?1:<\\)\\(?2:\\(?:.\\|\n\\)*?\\)\\(?3:>\\)\\)" `---- 3. C-x b file.html 4. M-x reb-change-syntax RET read RET 5. M-x re-builder RET 6. Copy paste the above regexp in to *RE-Builder* buffer 7. You will see the various components highlighted in HTML buffer 8. M-x reb-change-syntax RET string RET 9. You will see the above regexp changed from read syntax to string syntax. Something like. (Yes, the regexp is on two lines) "\(?:\(?1:<\)\(?2:\(?:.\| \)*?\)\(?3:>\)\)" 10. C-M-% Copy the above regexp without surrounding double quotes RET <\2> RET You are done. --