Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to make a regex substitution repeat until there are no more matches?

Reply
Thread Tools

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

 
 
David Deutsch
Guest
Posts: n/a
 
      02-14-2005
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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-14-2005
David Deutsch wrote:
> 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>

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Can't get subgroup of regex to repeat with +... what the ? Jon Ruby 8 05-18-2007 04:10 PM
Question on regex substitution using variables... Ian Perl 4 02-02-2006 10:49 PM
How to make a DVD in DVD Architect to repeat play? b.bowman DVD Video 5 10-23-2005 05:06 PM



Advertisments