From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "WJ" Newsgroups: gmane.emacs.help Subject: Re: Query replace regex with 2 alternatives Date: 8 Dec 2012 22:07:25 GMT Organization: NewsGuy - Unlimited Usenet $19.95 Message-ID: References: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1355004611 26717 80.91.229.3 (8 Dec 2012 22:10:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 8 Dec 2012 22:10:11 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Dec 08 23:10:24 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 1ThSbI-00012B-Bx for geh-help-gnu-emacs@m.gmane.org; Sat, 08 Dec 2012 23:10:24 +0100 Original-Received: from localhost ([::1]:34242 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ThSb5-0002FI-Ot for geh-help-gnu-emacs@m.gmane.org; Sat, 08 Dec 2012 17:10:11 -0500 Original-Path: usenet.stanford.edu!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!enews6 Original-Newsgroups: gnu.emacs.help Original-Lines: 65 Original-NNTP-Posting-Host: p424227a48343ec78a70960c945bce985c013ef0e2a665e4209547c4aaa9f6c24.newsdawg.com User-Agent: XanaNews/1.18.1.6 X-Antivirus: avast! (VPS 121207-1, 12/07/2012), Outbound message X-Antivirus-Status: Clean X-Received-Bytes: 2615 Original-Xref: usenet.stanford.edu gnu.emacs.help:195776 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:88094 Archived-At: Jambunathan K wrote: > 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. Where can the documentation for rx-to-string be found?