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

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.
  • Comment on Re: Re: Global regex giving up too soon

Replies are listed 'Best First'.
Re: Global regex giving up too soon
by Abigail-II (Bishop) on Jan 20, 2004 at 12:22 UTC
    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?

        There doesn't have to be a /g modifier. For some regex/string pairs, it doesn't matter whether there's a /g modifier. For others, it does.

        Abigail

        my $str = join ' ', map {"$_-PAT"} 1 .. 10; warn $str; 1 while $str =~ s/PAT/REPL/; die $str; __END__ 1-PAT 2-PAT 3-PAT 4-PAT 5-PAT 6-PAT 7-PAT 8-PAT 9-PAT 10-PAT at - line + 2. 1-REPL 2-REPL 3-REPL 4-REPL 5-REPL 6-REPL 7-REPL 8-REPL 9-REPL 10-REPL + at - line 4.
        An infinite loop with 1 while
        my $str = 'A b and A and c and A'; warn $str; $str =~ s/A/a/i for 1,2,3; die $str; __END__ A b and A and c and A at - line 3. a b and A and c and A at - line 5.
      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.