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

Discussion in 'Perl Misc' started by David Deutsch, Feb 14, 2005.

  1. 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
     
    David Deutsch, Feb 14, 2005
    #1
    1. Advertisements

  2. Re: How to make a regex substitution repeat until there are no morematches?

    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
     
    Gunnar Hjalmarsson, Feb 14, 2005
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Bour

    Extracting matches from Regex.Split

    Stephan Bour, Oct 29, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,596
    Stephan Bour
    Oct 30, 2003
  2. darrel
    Replies:
    1
    Views:
    833
    Blair Bonnett
    Jan 3, 2005
  3. Replies:
    4
    Views:
    1,700
  4. Replies:
    3
    Views:
    896
    Reedick, Andrew
    Jul 1, 2008
  5. Jon
    Replies:
    8
    Views:
    185
    Jon Fi
    May 18, 2007
Loading...

Share This Page