Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Query replace regex with 2 alternatives

15 views
Skip to first unread message

Dan Espen

unread,
Dec 7, 2012, 11:51:44 AM12/7/12
to

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

Barry Margolin

unread,
Dec 7, 2012, 1:59:06 PM12/7/12
to
In article <icd2ylg...@home.home>, Dan Espen <des...@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, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Dan Espen

unread,
Dec 7, 2012, 3:06:48 PM12/7/12
to
Barry Margolin <bar...@alum.mit.edu> writes:

> In article <icd2ylg...@home.home>, Dan Espen <des...@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

Jambunathan K

unread,
Dec 7, 2012, 3:11:25 PM12/7/12
to Dan Espen, help-gn...@gnu.org
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.



--

Tassilo Horn

unread,
Dec 7, 2012, 3:19:00 PM12/7/12
to help-gn...@gnu.org
Dan Espen <des...@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


Dan Espen

unread,
Dec 7, 2012, 4:32:10 PM12/7/12
to
And now, all of a sudden it penetrates my thick head.

Barry Margolin

unread,
Dec 8, 2012, 12:31:54 AM12/8/12
to
In article <ictxrxe...@home.home>, Dan Espen <des...@verizon.net>
wrote:

> Barry Margolin <bar...@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;") "[" "]")

WJ

unread,
Dec 8, 2012, 5:07:25 PM12/8/12
to
Where can the documentation for rx-to-string be found?

William Gardella

unread,
Dec 8, 2012, 6:03:13 PM12/8/12
to
"WJ" <w_a_...@yahoo.com> writes:

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

C-h f rx RET

--
I use grml (http://grml.org/)
0 new messages