in reply to Matching Regular expression at EOF

From the sounds of it, the entire file is in one variable? If so,
/^.*pat/s

Replies are listed 'Best First'.
Re^2: Matching Regular expression at EOF
by AnomalousMonk (Archbishop) on Feb 20, 2010 at 19:11 UTC

    Wouldn't anchoring the pattern at the end of the string with  \z or one of its ilk (e.g.,  m{ pat \z }xms) tend to be faster since the  .* doesn't need to 'consume' virtually the whole file? Or is this the kind of thing that just gets optimized away?

    Update: Contrary to krish28's clarification in Re^2: Matching Regular expression at EOF, this question assumes the entire file is held in a single string. But the question still stands.

      He wants the last instance of the pattern in the file. It might not at the very end of the file.

      As for speed, (?s:.*) jumps right to the end of the file. (?s:.) matches anything, so no check needs to be made (and none is done).

      Anchoring at the end of the string only matches if there's an actual match at the end of the string. But the last match doesn't imply it's at the end of the string. For instance, in the string below, there are two matches for /a.b/, on of them the last one, but none for /a.b\z/.
      "123 a!b 456 a?b 789"

        In my original persual of the OP, I missed the phrase "last occurence" [sic] and was beguiled by the phrases "Matching ... at EOF" and "match ... at the very end of the input file". Starting from my assumption that the entire file had been slurped into a string, I strayed into a consideration of  \z et al.

        I now recognize my error.

Re^2: Matching Regular expression at EOF
by krish28 (Acolyte) on Feb 20, 2010 at 19:10 UTC
    No... its not on a single variable... i am opening the file and reading it line by line...
      Then I assume the match doesn't span more than one line?
      my @match; while (<>)) { my @caps = /pat/ or next; @match = @caps; } if (@match) { print("Captured @match\n"); } else { die("No match\n"); }
      or
      my $match; while (<>)) { $match = $_ if /pat/; } if (defined($match)) { print("Matched $match"); } else { die("No match\n"); }

      All this guessing is leading to suboptimal solutions and wated work. If this is still not good, please provide more info about your problem.

      No... its not on a single variable... i am opening the file and reading it line by line...

      Try:

      my $last_line = undef;
      while( <$fh> ){
        $last_line = $_;
        # process file contents, if needed
      }
      if( defined $last_line ){
        $last_line =~ m/$pat\z/;
      }else{
        warn "file is empty\n";
      }
      
        That will erroneously claim the file is empty if the last occurrence of the pattern in on the second-last line (for example).