REGEXP: match every string *not* in group list

L

Laurent Schneider

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
 
D

Dr.Ruud

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'.
 
J

John W. Krahn

Laurent said:
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
 
L

Laurent Schneider

/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!"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top