a simple RegExp question

Discussion in 'Perl Misc' started by S. Levent Yilmaz, May 7, 2004.

  1. Hello,

    It sounded like a very simple problem but I couldn't come up with an
    elegant solution. The problem is:

    Find all lines which contain a given word (say 'kitty') not preceded
    with another given word (say 'puppy') . For instance:

    I have two pets:
    my kitty is very cute
    but puppy is cuter than kitty

    What regular expression stands only for the 2nd line? Note that the
    other way around is very easy, that is the lines with 'puppy' followed
    by 'kitty' is only "puppy.*kitty"
    Is this possible at all with a single RegExp?

    thank you so very much!
    -Levent.
     
    S. Levent Yilmaz, May 7, 2004
    #1
    1. Advertising

  2. S. Levent Yilmaz wrote:
    > Find all lines which contain a given word (say 'kitty') not
    > preceded with another given word (say 'puppy') . For instance:
    >
    > I have two pets:
    > my kitty is very cute
    > but puppy is cuter than kitty
    >
    > What regular expression stands only for the 2nd line? Note that the
    > other way around is very easy, that is the lines with 'puppy'
    > followed by 'kitty' is only "puppy.*kitty"
    > Is this possible at all with a single RegExp?


    Don't know, but why not just do:

    /\bkitty\b/ and !/\bpuppy\b.*\bkitty\b/

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 7, 2004
    #2
    1. Advertising

  3. In article <c7gs2a$n9e$>,
    "S. Levent Yilmaz" <> wrote:

    > Hello,
    >
    > It sounded like a very simple problem but I couldn't come up with an
    > elegant solution. The problem is:
    >
    > Find all lines which contain a given word (say 'kitty') not preceded
    > with another given word (say 'puppy') . For instance:
    >
    > I have two pets:
    > my kitty is very cute
    > but puppy is cuter than kitty
    >
    > What regular expression stands only for the 2nd line? Note that the
    > other way around is very easy, that is the lines with 'puppy' followed
    > by 'kitty' is only "puppy.*kitty"
    > Is this possible at all with a single RegExp?
    >
    > thank you so very much!
    > -Levent.


    You're looking for negative look-behind assertion. (I think).

    perldoc perlre

    look for "look-behind".

    Short form:

    my $line =~ m/(?<!puppy).*kitty/;

    HTH,
    Ricky

    --
    Pukku
     
    Richard Morse, May 7, 2004
    #3
  4. Richard Morse wrote:
    > You're looking for negative look-behind assertion. (I think).


    Think? How about testing the code before posting?

    > Short form:
    >
    > my $line =~ m/(?<!puppy).*kitty/;


    That matches any line containing "kitty".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 7, 2004
    #4
  5. S. Levent Yilmaz

    Matt Garrish Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > S. Levent Yilmaz wrote:
    > > Find all lines which contain a given word (say 'kitty') not
    > > preceded with another given word (say 'puppy') . For instance:
    > >
    > > I have two pets:
    > > my kitty is very cute
    > > but puppy is cuter than kitty
    > >
    > > What regular expression stands only for the 2nd line? Note that the
    > > other way around is very easy, that is the lines with 'puppy'
    > > followed by 'kitty' is only "puppy.*kitty"
    > > Is this possible at all with a single RegExp?

    >
    > Don't know, but why not just do:
    >
    > /\bkitty\b/ and !/\bpuppy\b.*\bkitty\b/
    >


    Or if the OP had bothered to read a bit of perlre:

    /\bkitty\b/ and $` !~ /\bpuppy\b/

    Matt
     
    Matt Garrish, May 7, 2004
    #5
  6. Purl Gurl wrote:
    > S. Levent Yilmaz wrote:
    >> Find all lines which contain a given word (say 'kitty') not
    >> preceded with another given word (say 'puppy') .

    >
    > You have failed to indicate if "kitty" followed by "puppy" is
    > permissible or to be ignored.


    How do you define "preceded"?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 8, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    177
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    542
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    404
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    213
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    202
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page