REGEXP: match every string *not* in group list

Discussion in 'Perl Misc' started by Laurent Schneider, Nov 14, 2005.

  1. I need to find every line containing abc followed by anything different
    than 123 and 456

    something like in shell the

    abc!(123|456)

    I tried the following :

    $ cat x
    abc123
    abc123 abc456
    abc456 abc789
    abc789
    abc453
    $ perl -ne
    '/abc([^14]..|.[^25].|..[^36]|126|153|156|423|426|453|.$|..$)/&&print'
    x
    abc456 abc789
    abc789
    abc453
    $

    which works ok for 123 and 456 but is a nightmare to code, especially
    with many groups of different lengths

    thanks for help
     
    Laurent Schneider, Nov 14, 2005
    #1
    1. Advertising

  2. Laurent Schneider

    Dr.Ruud Guest

    Laurent Schneider:

    > I need to find every line containing abc followed by anything
    > different than 123 and 456


    See `perldoc perlre`, look for 'negative look-ahead assertion'.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Nov 14, 2005
    #2
    1. Advertising

  3. Laurent Schneider wrote:
    > I need to find every line containing abc followed by anything different
    > than 123 and 456
    >
    > something like in shell the
    >
    > abc!(123|456)
    >
    > I tried the following :
    >
    > $ cat x
    > abc123
    > abc123 abc456
    > abc456 abc789
    > abc789
    > abc453
    > $ perl -ne
    > '/abc([^14]..|.[^25].|..[^36]|126|153|156|423|426|453|.$|..$)/&&print'
    > x
    > abc456 abc789
    > abc789
    > abc453
    > $


    $ echo "abc123
    abc123 abc456
    abc456 abc789
    abc789
    abc453" | perl -ne'print if /abc(?!123|456)/'
    abc456 abc789
    abc789
    abc453



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 14, 2005
    #3
  4. > /abc/ and not /abc(123|456)/
    will miss
    abc456 abc789

    > See `perldoc perlre`

    thanks a million! I just found it

    $ perl -ne '/abc(?!456|123|$)/&&print' x
    abc456 abc789
    abc789
    abc453
    abc45
    abc4

    and it works :)

    Just talking to myself : "never read the doc once. Read it at least
    twice!"
     
    Laurent Schneider, Nov 14, 2005
    #4
  5. thanks john
     
    Laurent Schneider, Nov 14, 2005
    #5
    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. Mikel Lindsaar
    Replies:
    0
    Views:
    506
    Mikel Lindsaar
    Mar 31, 2008
  2. Old Echo
    Replies:
    1
    Views:
    187
    Adam Shelly
    Sep 4, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    377
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
  5. Mark Anderson
    Replies:
    4
    Views:
    108
    Dr J R Stockton
    Mar 30, 2007
Loading...

Share This Page