How to make a regex substitution repeat until there are no more matches?

D

David Deutsch

I am trying to write a regular expression that will do a substitution
repeatedly until there are not more matches. Specifically, let's say I want
to convert every <option> tag in a select with name "YYY" into a link. If I
use:

s|(<select name="YYY">)<select.*?>(.*?)</select>|$1<a href =
"$2">$2</a>|gs

it will only change the first select (sorry if my syntax is not quite
correct). Is there a directive I could give in this regex that will do the
substitution over and over until there are no more patterns to substitute?

Thanks,
Dave
 
G

Gunnar Hjalmarsson

David said:
I am trying to write a regular expression that will do a substitution
repeatedly until there are not more matches. Specifically, let's say I want
to convert every <option> tag in a select with name "YYY" into a link. If I
use:

s|(<select name="YYY">)<select.*?>(.*?)</select>|$1<a href =
"$2">$2</a>|gs

it will only change the first select

Wasn't it <option> tags you wanted to change?

local $_ = <<HTML;
<select name="YYY">
<option value="1">1</option>
<option value="2">2</option>
</select>
HTML

s[(<select[^>]+name="YYY".+?</select>)][
my $select = $1;
$select =~ s|<option.*?>(.*?)</option>|<a href="$1">$1</a>|gis;
$select
]eis;

print;

Outputs:
<select name="YYY">
<a href="1">1</a>
<a href="2">2</a>
</select>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top