views:

171

answers:

2

I need to change the elements that surround some contents in a string, leaving the content as is. I'm doing it with a regular expression, but when I replace using the positional parameter ($1), it only uses the first match.

For example, if I have this string:

<strong>I want</strong> to change <i>this</i> text, 
but <u>I can't</u>. <div class="question">Why?</div>.

I want to change it to:

<strong>I want</strong> to change <i>this</i> text, 
but <u>I can't</u>. <div class="question">Why?</div>.

But I can only get this:

<strong>I want<strong> to change <strong>this<strong> text, 
but <strong>I can't<strong>. <strong>Why?<strong>.

I'm using the following regex:

\x26lt;(.*?)\x26gt;

(globally, and replacing it with "<$1>")

I guess the problem is I can't tell how many matches will be found, and how to refer to each one. Maybe in Perl I could have used $+, but that isn't working. I'm doing this with the regex module of Yahoo Pipes. It should be very similar to PHP implemenations, afaik.

How can I use each match separately in Yahoo Pipes?

+2  A: 

It might be easier to replace the &lt; and the &gt; independently. That way it becomes a simply find-replace that doesn't need to use back-references.

Ben S
would I need to do two passes for that? one to change the <s and another for the >s? (thanks!)
Fh
It would be two passes. Just do a string replace on < and > and then inject your new string into the web page.
mlevit
I'm not intimately familiar with Yahoo Pipes, but most regular expression engines have a replace all functionality of some sort. You would have to `replaceAll("<", "<")` and `replaceAll(">", ">")`, so yes, two operations would be required.
Ben S
Oh. Seems I always get too greedy when doing regular expressions. I was hoping there was a one liner I didn't know so I wouldn't have to do 2 passes. The back references was one hope that almost worked.
Fh
A: 
Alex Martelli