If the .sig must be the last entry then I'd probably use 'm/\.sig$/'
if you wanted to match the last part before the .sig (in the above case gpg
, then I'd write  m/(\.[^.]+\.sig)$/

That is
\. dot
[^.]+ one or more characters except dot
\.sig  .sig
$     at the end of the string


Gabor


On Tue, Aug 12, 2014 at 12:15 AM, Omer Zak <[email protected]> wrote:

> 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 ?
>
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to