Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to modify my regex?

by AnomalousMonk (Archbishop)
on Aug 26, 2015 at 19:05 UTC ( [id://1140101]=note: print w/replies, xml ) Need Help??


in reply to How to modify my regex?

What is wrong with my regex?

It's too greedy. Make  .* less greedy by adding the  ? "lazy" modifier to the  * "zero-or-more" quantifier to make it  .*? instead.

Update: See Quantifiers in perlre; also see discussions of greedy/lazy matching in perlretut. (Update: See also Quantifiers in regular expressions in Tutorials.)

Update 2: Example:

c:\@Work\Perl\monks\OldChamp>perl -wMstrict -le "my $s = 'keep me ZIP delete this ZAP also keep this ZIP kill too ZAP +keep too'; print qq{'$s'}; ;; (my $t = $s) =~ s{ ZIP .* ZAP }{}xmsg; print qq{greedy .*: '$t'}; ;; ($t = $s) =~ s{ ZIP .*? ZAP }{}xmsg; print qq{lazy .*?: '$t'}; " 'keep me ZIP delete this ZAP also keep this ZIP kill too ZAP keep too' greedy .*: 'keep me keep too' lazy .*?: 'keep me also keep this keep too'


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: How to modify my regex?
by OldChamp (Acolyte) on Aug 26, 2015 at 20:43 UTC

    Hi, AnomalousMonk, many thanks for helping me again, now my program works. It's really astonishing what this "lazy" modifier can change! I have read about greedy and lazy, but because of the lack of an example, I have not really understood what this means. Now I think I have at least an idea of this modifier and tomorrow I will visit the links you have supplied and hopefully I will become a little more knowlegeable.

      You're very welcome. It may seem a bit churlish to bring this up after your gracious thanks, but it dawned on me that the subject of the OP was familiar, and I was right: Re^3: Substitution don't work discusses a similar problem. Updates 2 & 3 there give contrasting examples of  .* versus  .*? (greedy versus lazy) behavior. (Also see the discussion there of the effect of the  /s regex modifier on the behavior of the  . (dot) metacharacter when matching against a multi-line string.)

      These comments are not intended to give you a hard time, but to suggest that the advice of the humble Monks will be of more value to you the more attention you pay it.


      Give a man a fish:  <%-{-{-{-<

        Thank you for your answer. I must confess, that at the time when reading your reply to 'Substitution don't work' I didn't understand this Updat 3, especially the meaning of the " ", and I had no clue what these [[ should mean. As I am just at the beginning with perl, I thought these characters are some special codes I just don't know yet, because there are so many characters with special meanings in perl. Now (that means today), after all the friendly posts of the humble Monks and after studying these posts and trying to understand what I read I finally have understood that between the " " that is the code and that the other stuff is meant to explain how the regex works. I eventually succeded in running this update 3 and I could see how it works. I agree, I should have tried harder the first time when I read this update 3. But for my defence I have to say that for experienced Monks like you all this is obvious, but for a newbie like me (72 years old :-) and as a German always fighting with my school English) even the obvious is sometimes not easy. I have learned a lot from you and the other monks since I have started here and I'm greatful to this community.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-23 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found