Dorf has asked for the wisdom of the Perl Monks concerning the following question:

I'm looping through the results of a glob, and then running the results through a regular expression to find parts of the file name with $1 and $2. The first result gets processed correctly (no matter which result it is), and the following regular expression checks fail to match. Below is my simplified code:
my %resultsHash; my @results = glob ( "//foo/bar/dead/*/*/report.html" ); for my $result (@results) { if ($result =~ ?//foo/bar/dead/(.*)/(.*)/report.html?) { $resultsHash{$2}[0] = $1; $resultsHash{$2}[1] = $result; } }
So, the 'if' statement passes on the first list element, and fails afterwards.

Retitled by Steve_p from 'Iterated Regulare Expression Failure'.

Replies are listed 'Best First'.
Re: Iterated Regular Expression Failure
by diotalevi (Canon) on May 04, 2005 at 19:13 UTC
    The ? delimiter is special. It means the expression is allowed to match only once, ever. That's exactly what you just described. It matches once and then never again. So change your delimiter. See also perlop on ?PATTERN?.
Re: Iterated Regular Expression Failure
by dragonchild (Archbishop) on May 04, 2005 at 19:13 UTC
    Try .*?, not .*. Or, even better, use File::Spec to break up the directory names. You don't need a regex there because you're not trying to winnow results - you know every result is going to match.

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"