in reply to Re: regex problem
in thread regex problem

I was just wondering how to parse out the jpgs links from reddit wallpaper page following a commandline fu bash script,just for fun. The main problem is how to reject a match which matches beginning and end but by using .+? matches falsely in the middle.I know you can reject matching letters with [^ahi] type expression.But I dont know how to reject a string in the middle of a large amount of data. The following shows the problem match.

http://b.thumbs.redditmedia.com/HUX1reWBCHSIQunAgKXYkb8nXEXY6cw0cTizkTcEw4U.png
random html etc alot
http://b.thumbs.redditmedia.com/bqYiA dIiTp01k7ca6UIjpWSJqOjHGeTv7JPwko4WrEQ.jpg

Rejecting the png but matching random file names in a mess of data is what Im struggling to do. Thanks for any help.This is just for fun so no sweat.

Replies are listed 'Best First'.
Re^3: regex problem solved
by grasshopper!!! (Beadle) on Nov 05, 2015 at 00:18 UTC

    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.

      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:  <%-{-{-{-<