Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: pattern matching a limited number of times

by PodMaster (Abbot)
on Sep 14, 2002 at 03:48 UTC ( [id://197782]=note: print w/replies, xml ) Need Help??


in reply to pattern matching a limited number of times

I try to keep code out of regexes, so i won't show you that example.
$foo =~ s/ab/cd/i for 1..5;

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: pattern matching a limited number of times
by bob49 (Initiate) on Sep 14, 2002 at 16:52 UTC
    $foo =~ s/ab/cd/i for 1..5;

    The above seems to work very well. I'm not sure I see the advantages to the more complex phrasings below. Please correct me if I'm wrong.

    Thanks Podmaster.
      The main problem is that it doesn't deal with overlapping regexes very well, (i.e. your regex matches parts of your replacement string).

      Suppose you have the string:

      "The wodchuck at the zo stod on the stoop";
      You realize that it will only make sense after replacing the first three 'o's with 'oo's
      my $str = "The wodchuck at the zo stod on the stoop"; $str =~ s/o/oo/i for 1..3; print "$str\n";
      Oops, that yields:
      The woooodchuck at the zo stod on the stoop
      The other solutions would have produced:
      The woodchuck at the zoo stood on the stoop

      -Blake

        Ok, I see Blakem... hmmmm.... I may be in the situation where the substitute will never be the same as the text substituted for..... Actually, I'm wrapping html around url's and their linked text.... I need to think about it though..... Thanks--you folks are really great.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://197782]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-23 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found