in reply to Specifying how many times a regex should work
Also, without the /g modifier on the match, you will simply match the same thing twice. The /g forces the next match to start looking where the last match left off.
Or you may be better off getting all matches at once by using /g in list context. Then you can just use a slice to get the ones you want, and not have to fuss around with a for loop. I think the readability is greatly improved.while (<FH>) { chomp; my $record = $_; for (0 .. 1) { $record =~ m/ ... /xg and print "matched: $1\n"; } }
while (<FH>) { chomp; my @all_matches = m/ ... /xg; my @first_few = @all_matches[0 .. 1]; }
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Specifying how many times a regex should work
by coldfingertips (Pilgrim) on Jun 05, 2004 at 19:48 UTC | |
by BrowserUk (Patriarch) on Jun 05, 2004 at 21:20 UTC |