in reply to one line regex eating CPU

while puts the match in a scalar context and loops until killed by some external factor (every time you try the match succeeds unless it never does).

Try this to achieve what you are after:

use strict; use warnings; my @titles; my $source = "<title>www.perlmonks.org</title><title>somewhere else</t +itle>"; push @titles, $source =~ m/<title>(.+?)<\/title>/ig; print join("\n", @titles);

Prints:

www.perlmonks.org somewhere else

Note too that you need to non-greedy match (.+?), and as you know, this will very likely come unstuck used on HTML. :)


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: one line regex eating CPU
by Anonymous Monk on Jun 23, 2006 at 20:11 UTC
    Thanks, that did fix it. But I swear I did this before where I did push(@array, $1) ... because I actually like that form better than push @array, ..

    How would this method match $1 AND $2 then (assuming we had a second capture going on)?

      Something like this may do what you want:

      use strict; use warnings; my @titles; my $source = <<TEXT; <title id='1'>www.perlmonks.org</title> <title id='2'>somewhere else</title> TEXT push @titles, $source =~ m/<title\s+id='(\d+)'>(.+?)<\/title>/igs; while (@titles) { my @pair = splice @titles, 0, 2; print "$pair[0]: $pair[1]\n"; }

      Prints:

      1: www.perlmonks.org 2: somewhere else

      DWIM is Perl's answer to Gödel