Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Puzzled by regex

by davido (Archbishop)
on Apr 10, 2013 at 06:16 UTC ( #1027893=note: print w/ replies, xml ) Need Help??


in reply to Puzzled by regex

There is a difference, but it's not necessarily in what is getting matched, but rather, in how it's matching. Example:

use strict; use warnings; my @strings = ( "____\n", "__ __\n", "__X __\n", "__Z__\n", "__\n__\n", "__^__\n", "_____\n", "________\n", ); foreach my $string ( @strings ) { print "<<$string>>\n"; if( $string =~ m/__(\S+?)__/ ) { print "\tNon-Greedy -- Match: (($1)).\n"; } else { print "\tNon-Greedy -- No Match.\n"; } if( $string =~ m/__(\S+)__/ ) { print "\tGreedy -- Match: [[$1]].\n"; } else { print "\tGreedy -- No Match.\n"; } }

Most of that is going to be pretty boring, until you get to the last item in the list, where you'll get the following output:

<<________ >> Non-Greedy -- Match: ((_)). Greedy -- Match: [[____]].

I have no idea whether non-greedy matching is going to have any practical effect in the type of strings you're matching with the regex though.


Dave


Comment on Re: Puzzled by regex
Select or Download Code
Re^2: Puzzled by regex
by syphilis (Canon) on Apr 10, 2013 at 07:17 UTC
    Yes, but you forgot to put the trailing \n into the two regexes :-)
    If I put it in, that makes the last one match as well:
    Non-Greedy -- Match: ((____)). Greedy -- Match: [[____]].
    Thanks for the replies guys.
    I'm about to mess with that code, but I was loathe to do that while I couldn't see why the ? had been included in the regex. I still don't see why it's there - but at least now I'm starting to feel a little confident that it serves no purpose. (I'll still probably leave it there ... because I'm feeling even more confident that it doesn't do any harm :-)

    Cheers,
    Rob

      but at least now I'm starting to feel a little confident that it serves no purpose. (I'll still probably leave it there ... because I'm feeling even more confident that it doesn't do any harm :-)

      Its probably a reflex :) I know when I write regex I make more mistakes from greedines than from non-greediness, so I tend to write +? *? to be on the safe side

      I know I'm not alone in getting bit by it , it is a frequent cause/solution from newbies

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027893]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-07-11 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (222 votes), past polls