in reply to Re: Re: Global regex giving up too soon
in thread Global regex giving up too soon

Yes, of course. Consider:
#!/usr/bin/perl use strict; use warnings; my $str = "1" x 5; $_ = $str; print "Single s///g:\n"; print "Before: [$_]; "; s/1(1+)/$1/g; print "After [$_]\n"; $_ = $str; print "Loop s///:\n"; while (1) { print "Before: [$_]; "; last unless s/1(1+)/$1/; print "After: [$_]\n"; } print "\n"; __END__ Single s///g: Before: [11111]; After [1111] Loop s///: Before: [11111]; After: [1111] Before: [1111]; After: [111] Before: [111]; After: [11] Before: [11]; After: [1] Before: [1];

Abigail

Replies are listed 'Best First'.
Re: Re: Global regex giving up too soon
by Wassercrats (Initiate) on Jan 20, 2004 at 12:10 UTC
    Thanks for the demo. I better review all my uses of /g. Why does it bother me that there isn't a modifier that could replace the loop in these cases? Probably because I could use one, and because there are so many other shortcuts in Perl.
      There is standard idiom for it:
      1 while $str =~ s/PAT/REPL/;
      Abigail
        1 while $str =~ s/PAT/REPL/;

        Is the idiom supposed to contain the /g modifier? You didn't include it and I can't say I've ever seen 1 while s/foo/bar/, only 1 while s/foo/bar/g;. So tell me, should there be a /g there or am I in for a new lesson in the regex classroom?

        I'll have to figure out if I'd remember what that means or if I should check $&. I think I got it. Ugly though.