Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

there's a funny regex in the source of TWiki we're trying to use here (and failing - that's why I'm going through the source) where it parses an RCS file:

/^([0-9]+)\.([0-9]+)\s+date\s+(\d\d(\d\d)?(\.\d\d){5}?);$/o
note the last part:
(\.\d\d){5}?;
I've never seen quantifiers strung together like that... I've checked the Camel Book - it doesn't specifically cover this case, but its talk about "quantified atoms" seems to suggest that the construct is illegal. OTOH since it does compile, so I would be tempted to interpret it as
((\.\d\d){5})?;
- but that's not how it matches... :-( It does match
.01.02.03.04.05;
but not a lone
;
I get a feeling there's something wrong with this regex - what do you think?

Replies are listed 'Best First'.
Re: Regex quantifiers composed?
by blakem (Monsignor) on Oct 11, 2002 at 10:14 UTC
    The question mark after a quantifier turns off greeding matching. In your regex it doesn't make much sense though. You're trying to match exactly 5 times, so greedy and non-greedy are the same thing.
    /a{1,5}/; # match 1-5 'a's prefer the longest. /a{1,5}?/; # match 1-5 'a's prefer the shortest.
    Here is an example:
    #!/usr/bin/perl -wT use strict; $_ = 'aaaab'; # greedily match upto 5 'a's followed by a word char print "Greedy: "; print /(a{1,5})\w/, "\n"; # non-greedily match upto 5 'a's followed by a word char print "NonGreedy: "; print /(a{1,5}?)\w/, "\n"; __END__ Greedy: aaaa NonGreedy: a

    -Blake

Re: Regex quantifiers composed?
by PodMaster (Abbot) on Oct 11, 2002 at 11:36 UTC
    I know blakem explained it, but when in doubt,

    YAPE::Regex::Explain to the rescue

    use YAPE::Regex::Explain; my $regex = '/^([0-9]+)\.([0-9]+)\s+date\s+(\d\d(\d\d)?(\.\d\d){5}?);$ +/o'; #print YAPE::Regex::Explain->new($regex)->explain; $regex = '(\.\d\d){5}?;'; print '#' x 60,"\n"; print YAPE::Regex::Explain->new($regex)->explain; print '#' x 60,"\n"; print YAPE::Regex::Explain->new(qr/a{1,5}/)->explain; print '#' x 60,"\n"; print YAPE::Regex::Explain->new(qr/a{1,5}?/)->explain; __END__
Re: Regex quantifiers composed?
by Anonymous Monk on Oct 11, 2002 at 10:59 UTC

    Thanks!

    Another question: what does the /o modifier for the original regex actually do? I've looked it up, and Mark-Jason Dominus says it's an optimization for regexes that interpolate variables - but there aren't any variables in the TWiki regex...

      heh heh, yep the /o is pointless as well.

      -Blake