in reply to Re: Grabbing part of an HTML page
in thread Grabbing part of an HTML page

For the single regex to work, you would need to add the "s" modifier at the end, so that your ".*" doesn't stop matching at the first line-break character. Also, depending on the nature of the OP's data, it may need to be a non-greedy match:
# ... if ( $file =~ /$start_pattern(.*?)$end_pattern/s ) { print $1; } # ...
update: Forgot to mention: if the OP's data happens to contain more than one "start ... end" sequence within the same file, this would have to be structured as a loop -- something like:
# while ( $file =~ /start_pattern(.*?)$end_pattern/gs ) { print "$1\n"; } #

Replies are listed 'Best First'.
Re: Re: Re: Grabbing part of an HTML page
by cLive ;-) (Prior) on Mar 29, 2004 at 04:58 UTC

    What does the /s modifier do if $/ is undefined? :)

    cLive ;-)

      Well, the current value of $/ has nothing to do with whether or not "." in a regex will match "\n" in a given string value; only the "s" modifier on the regex will allow "." to match "\n", no matter what $/ is. Try this out:
      #!/usr/bin/perl $/ = undef; $_ = <DATA>; while ( /begin(.*?)end/g ) { print "\n--- found without 's': ---\n$1\n"; } while ( /begin(.*?)end/gs ) { print "\n--- found using 's': ---\n$1\n"; } __DATA__ blah begin foo bar baz end begin foo2 bar2 baz2 end
        D'oh - I just checked the file I tested on and it doeasn't contain a single new line.

        No wonder I was confused.

        cLive ;-)