Hello Assaf,
A non-greedy match starts matching at the leftmost possible position.
If I add '.*' at beginning of the regular expression, it would match
only '.sig':
$ echo "file.tar.gz.gpg.sig" | perl -lne 'm/.*(\..*?sig)/ && print $1'

By the way, why are you matching (\..*?sig) rather than a simple
(\.sig) ?

You may want to see also the following discussion:
http://stackoverflow.com/questions/15191291/make-a-non-greedy-regex-in-backward-direction-to-behave-the-same-like-in-forward

--- Omer




On Mon, 2014-08-11 at 17:30 -0400, Assaf Gordon wrote:
> Hello,
> 
> A small greedy/non-greedy regex question for you.
> I'm sure it's something simple, but I can't figure it out.
> 
> Given the following:
> 
>      $echo "file.tar.gz.gpg.sig" | perl -lne 'm/(\..*?sig)/ && print $1'
>      .tar.gz.gpg.sig
> 
> Why does the regex match all the extensions ?
> The regex requires:
>    \.  = actual dot character
>    .*? = zero or more characters, non-greedy
>    sig = the string "sig".
> 
> I'd naively assume that "zero or more, non-greedy" should match zero 
> characters,
> and the result should be just ".sig" .
> 
> What am I doing wrong ?
-- 
Any legal limit to self defense means that there is no right for self
defense at all.  This is because the aggressors would exploit those
legal limits to render their victims totally defenseless.
My own blog is at http://www.zak.co.il/tddpirate/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with which
I may be affiliated in any way.
WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html

_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to