Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

a regular expression question

by brassbin (Novice)
on Dec 18, 2012 at 15:05 UTC ( #1009391=perlquestion: print w/replies, xml ) Need Help??
brassbin has asked for the wisdom of the Perl Monks concerning the following question:


i'm processing a netgroup file, trying to delete entries from the netgroup file, here's the issue i have

so say i have this pattern in the netgroup file

ng1 (-,bob,) (-,dick,) (,jane,) (,bob,)

so i want to match the entries "bob" and delete it, here's what i use


i do want to use the "g" flag to match multiple instances, i thought it would match the shortest possible patterns, but it does the opposite, it matches the widest pattern, from the first instance of "bob" all the way to the end of the line, matching the ")" at the end of the line

what am i doing wrong?

Thank you very much for your help!

Replies are listed 'Best First'.
Re: a regular expression question
by toolic (Bishop) on Dec 18, 2012 at 15:12 UTC

      i know the netgroup file i'm processing probably doesn't have 3 spaces before the first comma, what if it does?

      i thought regular expression matches the short possible pattern, not the longest, am i wrong?


        As toolic mentioned, the .* construct is greedy and tries to get the longest match. If you turn it into .*?, it then looks for the shortest match. If you read the link he provided, you could verify that yourself.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009391]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2018-06-25 18:12 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.