in reply to Regex isn't performing like I think it should

Like chromatic said, it is the .+ that is messing you up. You can make that non-greedy by appending a ?. So, your example might become:
while ( $string =~ m!(<a[^>]*>)(.+?</a[^>]*>)!ig ) { print "$1 \n"; print "$2 \n"; }
I tried that against a portion of your example string (after replacing the <'s and >'s), and it seemed to work ok. I'm pretty much a newbie, so I imagine there are better ways to do it then I have described. Edit: By the way, it's the "g" option on the regex that makes it work in that "while". I guess when it tries to do a multiple match, it will eventually fall off the end, and return a false. Without the "g", that while statement I used will run forever, as long as the string contains a single match to your regexp.

Replies are listed 'Best First'.
Re: Re: Regex isn't performing like I think it should
by Popcorn Dave (Abbot) on Jun 12, 2002 at 04:44 UTC
    Thanks for that! I'm not that far beyond newbie myself :)

    That does work, but it still fails to get anything but the first match even though I'm telling it to do it globally ( or at least I think I am ). That's the part that's still throwing me.

    Some people fall from grace. I prefer a running start...

      Did you try the using "while", as well? I've got this:
      #!/usr/bin/perl -w my $string = '<a href="page.cfm?objectid=11933900&method=full&siteid=5 +0144"> Costly false alarms </a> <a href="page.cfm?objectid=11933890&m +ethod=full&siteid=50144"> Mindless yobs terrorise OAPs </a> <a href=" +page.cfm?objectid=11933879&method=full&siteid=50144"> Road deaths </a +> <a href="page.cfm?objectid=11933842&method=full&siteid=50144"> Twis +ted porn pervert caged for life </a> <a href="page.cfm?objectid=11933 +800&method=full&siteid=50144"> Greenbelt homes plan appeal thrown out + </a>'; while ( $string =~ m!(<a[^>]*>)(.+?</a[^>]*>)!ig ) { print "1st match: $1 \n"; print "2nd match: $2 \n\n"; }
      and when I run it, I get this:
      1st match: <a href="page.cfm?objectid=11933900&method=full&siteid=5014 +4"> 2nd match: Costly false alarms </a> 1st match: <a href="page.cfm?objectid=11933890&method=full&siteid=5014 +4"> 2nd match: Mindless yobs terrorise OAPs </a> 1st match: <a href="page.cfm?objectid=11933879&method=full&siteid=5014 +4"> 2nd match: Road deaths </a> 1st match: <a href="page.cfm?objectid=11933842&method=full&siteid=5014 +4"> 2nd match: Twisted porn pervert caged for life </a> 1st match: <a href="page.cfm?objectid=11933800&method=full&siteid=5014 +4"> 2nd match: Greenbelt homes plan appeal thrown out </a>
      which I think is what you want. I first tried just doing a single "$string =~ m!(<a^>*>)(.+?</a^>*>)!ig" and then printing out $1, $2,...$8, and only got values for the first two. That "while" thing seems a little shady, for the reason I put in my updated post above, but it seems to work in this case.
      What you're running into here is a matter of context. You're evaluating the regex in scalar context, which, when used with the /g modifier, performs a progressive match. This means that every time that regex is evaluated, it starts trying to match immediately after the previous. In other words, it keeps track of it's position in the string and starts where it left off.

      One solution to this is to use a while loop to take advantage of the progressive matching (every time the /g regex is eval'd you get the next values for $1 and $2) as jmarshall99 did. The other is to evaluate your regex in list context to rip out all the matches at once.

      my $string = '<a href="page.cfm?objectid=11933900&method=full&siteid=5 +0144"> Costly false alarms </a> <a href="page.cfm?objectid=11933890&m +ethod=full&siteid=50144"> Mindless yobs terrorise OAPs </a> <a href=" +page.cfm?objectid=11933879&method=full&siteid=50144"> Road deaths </a +> <a href="page.cfm?objectid=11933842&method=full&siteid=50144"> Twis +ted porn pervert caged for life </a> <a href="page.cfm?objectid=11933 +800&method=full&siteid=50144"> Greenbelt homes plan appeal thrown out + </a>'; my @matches = $string =~ m!(<a[^>]*>)(.+?</a[^>]*>)!ig; print "$_\n" foreach @matches;
      Or even using a hash (as long as your a tags don't repeat)...
      my %matches = $string =~ m!(<a[^>]*>)(.+?</a[^>]*>)!ig; foreach my $key (keys %matches) { print "$key\n"; print "$matches{$key}\n\n"; }

      -Bird

        ++ for that!!!

        I'd never even thought of, or even seen for that matter, using the array to grab all matches. Excellent technique!

        As far as using a hash, that's actually what it's going in to just for that very reason - to eliminate dups. The problem is, with this particular HTML, there are duplicate links with the news headline as one link, the associated text, then 'more' as a duplicate link underneath the associated text.

        Again, ++ ++ for that array idea! Thanks!

        Some people fall from grace. I prefer a running start...

Re: Re: Regex isn't performing like I think it should
by dsheroh (Monsignor) on Jun 12, 2002 at 16:46 UTC
    A different (and, I suspect, more efficient) way to handle this would be to use another negated character class instead of the non-greedy modifier:
    while ( $string =~ m!(<a[^>]*>)([^<]+</a[^>]*>)!ig )
    (Personally, I'd make that [^<]* instead of +, just in case you get data with links <right><up><against><each><other>, but that's your call.)