Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

why my reg ex matches greedy?

by lngperl (Initiate)
on Jun 25, 2012 at 20:06 UTC ( [id://978257]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,
use strict; use warnings; my $str = '6AG3_608_FGG_20100124_20120923418_20126254447_201262512026. +txt'; $str =~ m/(.*)(_\d{11,}?.*)(\.\w+)/; print $1, "\n"; print $2, "\n"; print $3, "\n";

prints 6AG3_608_FGG_20100124_20120923418_20126254447
_201262512026
.txt


But i want it to print
6AG3_608_FGG_20100124
_20120923418_20126254447_201262512026
.txt


Basically the filename gets appened with "_timestamp" after the YYYMMDD part everytime when it gets processed. i want to strip out the time stamps alone.


What i am doing wrong here?

Any help is much appreciated and thanks.

Replies are listed 'Best First'.
Re: why my reg ex matches greedy?
by tobyink (Canon) on Jun 25, 2012 at 21:33 UTC

    The (.*) part is greedy because you've not used the ungreedy quantifier. The ungreedy version is (.*?).

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: why my reg ex matches greedy?
by McA (Priest) on Jun 25, 2012 at 20:44 UTC
    Hi,
    probably that is what you want:
    use strict; use warnings; my $str = '6AG3_608_FGG_20100124_20120923418_20126254447_201262512026. +txt'; $str =~ s/_\d{11,}//g; print $str, "\n";
      hi McA, thanks. yes that is what eventually i would need. but i would like to understand the reg ex to extract the pattern.
        Ok ok ok,
        then take this:
        use strict; use warnings; my $str = '6AG3_608_FGG_20100124_20120923418_20126254447_201262512026. +txt'; $str =~ m/(.*?)(_\d{11,}?.*)(\.\w+)/; print $1, "\n"; print $2, "\n"; print $3, "\n";
Re: why my reg ex matches greedy?
by roboticus (Chancellor) on Jun 25, 2012 at 20:19 UTC

    Inperl:

    You can't just make up syntax and expect perl to know what you want. Read perlre, then read it a couple more times, then you should be able to figure out how to tell perl what you're looking for.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      i did not make my own syntax and expect perl to understand it. it is just that i do not know what i was doing wrong over there and trying to seek help.

        Well, clearly I'm an idiot. I thought I'd drop a mention of YAPE::Regex::Explain to show how to figure out what a regular expression is saying, so I could point out the "invented" bit. But I find that the bit I thought was "invented" seems fine. Sorry about that. When I run:

        use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(qr/(.*)(_\d{11,}?.*)(\.\w+)/)->explain +();

        I get:

        And I thought the \d{11,}? was the invented bit. I'll have to play with that sometime.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://978257]
Approved by muba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found