Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: match and regex

by Shendal (Hermit)
on Aug 13, 2002 at 19:34 UTC ( #189924=note: print w/ replies, xml ) Need Help??

in reply to match and regex

Use a zero-width negative lookahead assertion:

#!/usr/bin/perl -w use strict; foreach (<DATA>) { print "$1\n" if (m|(\w+\.bld)(?!\S*#)|) } __DATA__ ../nbssbase/ip/bld/l?ibipGH040.bld:57:# Reflecting change of eni.cxx ../nbssbase/ip/bld/l?ibipGH060.bld:60:# Reflecting change of eni.cxx ../nbssbase/ip/bld/l?ib.bld:17:eni.cxx


Comment on Re: match and regex
Download Code
Replies are listed 'Best First'.
Re: Re: match and regex
by splitOnce (Acolyte) on Aug 13, 2002 at 19:43 UTC
    thanks ,, it works , would you mind explaining this part to me :
      I could, but I think that perldoc perlre can give a better explanation:
      (?!pattern) A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of "foo" that isn't followed by "bar". Note however that lookahead and lookbehind are NOT the same thing. You cannot use this for lookbehind. If you are looking for a "bar" that isn't preceded by a "foo", /(?!foo)bar/ will not do what you want. That's because the (?!foo) is just saying that the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will match. You would have to do something like /(?!foo) for that. We say "like" because there's the case of your "bar" not having three characters before it. You could cover that this way: /(?:(?!foo)...|^.{0,2})bar/. Sometimes it's still easier just to say: if (/bar/ && $` !~ /foo$/)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2015-10-10 06:44 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (255 votes), past polls