Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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:

hi,

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

s/\(.*,\s*bob\s*,.*\)//g

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!

Comment on a regular expression question
Download Code
Re: a regular expression question
by toolic (Chancellor) 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?

        brassbin:

        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.

        ...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
Node Status?
node history
Node Type: perlquestion [id://1009391]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-12-27 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls