in reply to Re^2: regex problem
in thread regex problem

Have solved problem but I dont understand why one lookahead works and another does not.

use strict; use warnings; use WWW::Curl::Easy; my $curl = WWW::Curl::Easy->new; $curl->setopt(CURLOPT_HEADER,1); $curl->setopt(CURLOPT_URL, 'http://www.reddit.com/r/wallpapers.rss'); my $response_body; $curl->setopt(CURLOPT_WRITEDATA,\$response_body); # Starts the actual request my $retcode = $curl->perform; # Looking at the results... if ($retcode == 0){ print("Transfer went ok\n\n"); my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE); my @urls=$response_body =~ m{(http://b.thumbs.redditmedia\.com/(?:( +?!png).)*?\.jpg)}gi; $" ="\n\n"; print "@urls\n"; } else { # Error code, type of error, error message print("An error happened: $retcode ".$curl->strerror($retcode)." ".$ +curl->errbuf."\n"); }

Thank you all that helped.

Replies are listed 'Best First'.
Re^4: regex problem solved
by AnomalousMonk (Archbishop) on Nov 05, 2015 at 00:48 UTC

    The regex
        (?!pattern).
    simply says that whatever  . matches is not the start of whatever  pattern matches (which can be any regex whatsoever). Wrapping this in a non-capturing group
        (?:(?!pattern).)
    allows you to quantify the grouped expression in the usual way, so
        (?:(?!pattern).)*?
    means "zero or more of anything as long as it doesn't begin a  pattern sequence, with lazy instead of greedy matching". (And I'll go to my grave using "lazy" instead of "non-greedy" as the antonym of "greedy" matching.)

    Here's what YAPE::Regex::Explain sez, but I don't think it captures the essence of the concept as clearly:

    c:\@Work\Perl\monks>perl -wMstrict -le "use YAPE::Regex::Explain; ;; print YAPE::Regex::Explain->new('(?:(?!pattern).)*?')->explain; " The regular expression: (?-imsx:(?:(?!pattern).)*?) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?: group, but do not capture (0 or more times (matching the least amount possible)): ---------------------------------------------------------------------- (?! look ahead to see if there is not: ---------------------------------------------------------------------- pattern 'pattern' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- )*? end of grouping ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------


    Give a man a fish:  <%-{-{-{-<