unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Query replace regex with 2 alternatives
@ 2012-12-07 16:51 Dan Espen
  2012-12-07 18:59 ` Barry Margolin
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Dan Espen @ 2012-12-07 16:51 UTC (permalink / raw)
  To: help-gnu-emacs


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.

-- 
Dan Espen


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
  2012-12-07 16:51 Query replace regex with 2 alternatives Dan Espen
@ 2012-12-07 18:59 ` Barry Margolin
  2012-12-07 20:06   ` Dan Espen
  2012-12-07 20:11 ` Jambunathan K
       [not found] ` <mailman.14799.1354910918.855.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 9+ messages in thread
From: Barry Margolin @ 2012-12-07 18:59 UTC (permalink / raw)
  To: help-gnu-emacs

In article <icd2ylg5zz.fsf@home.home>, Dan Espen <despen@verizon.net> 
wrote:

> Could use some help on query/replace/regex.
> 
> I have an html file full of &lt; and &gt;.
> I want to replace only some of the pairs with "[" and "]".
> 
> I figured out the match string:
> 
> "\\(&lt;\\|&gt;\\)
> 
> (typed as)
> 
> "\(&lt;\|&gt;\)
> 
> but when it comes to the replacement, I'm not clear on how to say,
> first match gets [ and second match gets ].

Do two separate query-replaces, one to replace &lt; with [, the other to 
replace &gt; with ].

> 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.

I think what you're talking about is:

&lt;\(.*?\)&gt;
Relace with:
[\1]

Or maybe this is what you mean:

&lt;\|&gt;
Replace with:
\,(if (string-equal \& "&lt;" "[" "]"))

This latter option is only available in interactive mode, not when 
calling query-replace-regexp from Elisp.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
  2012-12-07 18:59 ` Barry Margolin
@ 2012-12-07 20:06   ` Dan Espen
  2012-12-07 20:19     ` Tassilo Horn
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Dan Espen @ 2012-12-07 20:06 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

> In article <icd2ylg5zz.fsf@home.home>, Dan Espen <despen@verizon.net> 
> wrote:
>
>> Could use some help on query/replace/regex.
>> 
>> I have an html file full of &lt; and &gt;.
>> I want to replace only some of the pairs with "[" and "]".
>> 
>> I figured out the match string:
>> 
>> "\\(&lt;\\|&gt;\\)
>> 
>> (typed as)
>> 
>> "\(&lt;\|&gt;\)
>> 
>> but when it comes to the replacement, I'm not clear on how to say,
>> first match gets [ and second match gets ].
>
> Do two separate query-replaces, one to replace &lt; with [, the other to 
> replace &gt; with ].
>
>> 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.
>
> I think what you're talking about is:
>
> &lt;\(.*?\)&gt;
> Relace with:
> [\1]

Thanks Barry, that worked for what I wanted and is perhaps better.

> Or maybe this is what you mean:
>
> &lt;\|&gt;
> Replace with:
> \,(if (string-equal \& "&lt;" "[" "]"))

This is what I was asking for.
I pasted both values into emacs and the latter part gave me an
error, (wrong-number-of-arguments if 1).  The same error I get if
I try to evaluate the expression.  Not important now, I got the idea.

I don't see "\," here:

http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Backslash.html#Regexp-Backslash

http://tinyurl.com/aqod6fv

okay, I do see it here:

http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Replace.html#Regexp-Replace

It would have helped if I looked at the current documentation...

Thanks again.

-- 
Dan Espen


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
  2012-12-07 16:51 Query replace regex with 2 alternatives Dan Espen
  2012-12-07 18:59 ` Barry Margolin
@ 2012-12-07 20:11 ` Jambunathan K
       [not found] ` <mailman.14799.1354910918.855.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 9+ messages in thread
From: Jambunathan K @ 2012-12-07 20:11 UTC (permalink / raw)
  To: Dan Espen; +Cc: help-gnu-emacs

Dan Espen <despen@verizon.net> writes:

> Could use some help on query/replace/regex.
>
> I have an html file full of &lt; and &gt;.
> I want to replace only some of the pairs with "[" and "]".
>
> I figured out the match string:
>
> "\\(&lt;\\|&gt;\\)
>
> (typed as)
>
> "\(&lt;\|&gt;\)
>
> 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 "&lt;") 
                        (group-n 2 (minimal-match
                                    (zero-or-more anything)))
                        (group-n 3 "&gt;")))

   Here is a sample session.

    ,----
    | ELISP> (rx-to-string '(and (group-n 1 "&lt;") 
    | 		    (group-n 2 (minimal-match
    | 				(zero-or-more anything)))
    | 		    (group-n 3 "&gt;")))
    | "\\(?:\\(?1:&lt;\\)\\(?2:\\(?:.\\|\n\\)*?\\)\\(?3:&gt;\\)\\)"
    `----

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:&lt;\)\(?2:\(?:.\|
\)*?\)\(?3:&gt;\)\)"

10. C-M-% 
    Copy the above regexp without surrounding double quotes RET
    <\2> RET

You are done.



-- 



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
  2012-12-07 20:06   ` Dan Espen
@ 2012-12-07 20:19     ` Tassilo Horn
       [not found]     ` <mailman.14801.1354911562.855.help-gnu-emacs@gnu.org>
  2012-12-08  5:31     ` Barry Margolin
  2 siblings, 0 replies; 9+ messages in thread
From: Tassilo Horn @ 2012-12-07 20:19 UTC (permalink / raw)
  To: help-gnu-emacs

Dan Espen <despen@verizon.net> writes:

>> Or maybe this is what you mean:
>>
>> &lt;\|&gt;
>> Replace with:
>> \,(if (string-equal \& "&lt;" "[" "]"))
>
> This is what I was asking for.
> I pasted both values into emacs and the latter part gave me an
> error, (wrong-number-of-arguments if 1).

A paren is wrong.  It has to be

  \,(if (string-equal \&) "&lt;" "[" "]")

Bye,
Tassilo




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
       [not found]     ` <mailman.14801.1354911562.855.help-gnu-emacs@gnu.org>
@ 2012-12-07 21:32       ` Dan Espen
  0 siblings, 0 replies; 9+ messages in thread
From: Dan Espen @ 2012-12-07 21:32 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn <tsdh@gnu.org> writes:

> Dan Espen <despen@verizon.net> writes:
>
>>> Or maybe this is what you mean:
>>>
>>> &lt;\|&gt;
>>> Replace with:
>>> \,(if (string-equal \& "&lt;" "[" "]"))
>>
>> This is what I was asking for.
>> I pasted both values into emacs and the latter part gave me an
>> error, (wrong-number-of-arguments if 1).
>
> A paren is wrong.  It has to be
>
>   \,(if (string-equal \&) "&lt;" "[" "]")

And now, all of a sudden it penetrates my thick head.

Thanks again.

-- 
Dan Espen


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
  2012-12-07 20:06   ` Dan Espen
  2012-12-07 20:19     ` Tassilo Horn
       [not found]     ` <mailman.14801.1354911562.855.help-gnu-emacs@gnu.org>
@ 2012-12-08  5:31     ` Barry Margolin
  2 siblings, 0 replies; 9+ messages in thread
From: Barry Margolin @ 2012-12-08  5:31 UTC (permalink / raw)
  To: help-gnu-emacs

In article <ictxrxeief.fsf@home.home>, Dan Espen <despen@verizon.net> 
wrote:

> Barry Margolin <barmar@alum.mit.edu> writes:
> > &lt;\|&gt;
> > Replace with:
> > \,(if (string-equal \& "&lt;" "[" "]"))
> 
> This is what I was asking for.
> I pasted both values into emacs and the latter part gave me an
> error, (wrong-number-of-arguments if 1).  The same error I get if
> I try to evaluate the expression.  Not important now, I got the idea.

Sorry, I misplaced a parenthesis, it should be:

\,(if (string-equal \& "&lt;") "[" "]")

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
       [not found] ` <mailman.14799.1354910918.855.help-gnu-emacs@gnu.org>
@ 2012-12-08 22:07   ` WJ
  2012-12-08 23:03     ` William Gardella
  0 siblings, 1 reply; 9+ messages in thread
From: WJ @ 2012-12-08 22:07 UTC (permalink / raw)
  To: help-gnu-emacs

Jambunathan K wrote:

> Dan Espen <despen@verizon.net> writes:
> 
> > Could use some help on query/replace/regex.
> > 
> > I have an html file full of &lt; and &gt;.
> > I want to replace only some of the pairs with "[" and "]".
> > 
> > I figured out the match string:
> > 
> > "\\(&lt;\\|&gt;\\)
> > 
> > (typed as)
> > 
> > "\(&lt;\|&gt;\)
> > 
> > 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 "&lt;") 
>                         (group-n 2 (minimal-match
>                                     (zero-or-more anything)))
>                         (group-n 3 "&gt;")))
> 
>    Here is a sample session.
> 
>     ,----
>     | ELISP> (rx-to-string '(and (group-n 1 "&lt;") 
>     | 		    (group-n 2 (minimal-match
>     | 				(zero-or-more anything)))
>     | 		    (group-n 3 "&gt;")))
>     | "\\(?:\\(?1:&lt;\\)\\(?2:\\(?:.\\|\n\\)*?\\)\\(?3:&gt;\\)\\)"
>     `----
> 
> 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:&lt;\)\(?2:\(?:.\|
> \)*?\)\(?3:&gt;\)\)"
> 
> 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?


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Query replace regex with 2 alternatives
  2012-12-08 22:07   ` WJ
@ 2012-12-08 23:03     ` William Gardella
  0 siblings, 0 replies; 9+ messages in thread
From: William Gardella @ 2012-12-08 23:03 UTC (permalink / raw)
  To: help-gnu-emacs

"WJ" <w_a_x_man@yahoo.com> writes:

> Where can the documentation for rx-to-string be found?

C-h f rx RET

-- 
I use grml (http://grml.org/)


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-12-08 23:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-07 16:51 Query replace regex with 2 alternatives Dan Espen
2012-12-07 18:59 ` Barry Margolin
2012-12-07 20:06   ` Dan Espen
2012-12-07 20:19     ` Tassilo Horn
     [not found]     ` <mailman.14801.1354911562.855.help-gnu-emacs@gnu.org>
2012-12-07 21:32       ` Dan Espen
2012-12-08  5:31     ` Barry Margolin
2012-12-07 20:11 ` Jambunathan K
     [not found] ` <mailman.14799.1354910918.855.help-gnu-emacs@gnu.org>
2012-12-08 22:07   ` WJ
2012-12-08 23:03     ` William Gardella

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).