Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > REGEXP: match every string *not* in group list

Reply
Thread Tools

REGEXP: match every string *not* in group list

 
 
Laurent Schneider
Guest
Posts: n/a
 
      11-14-2005
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

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      11-14-2005
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."
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-14-2005
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
 
Reply With Quote
 
Laurent Schneider
Guest
Posts: n/a
 
      11-14-2005
> /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!"

 
Reply With Quote
 
Laurent Schneider
Guest
Posts: n/a
 
      11-14-2005
thanks john

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Match every word except a substring T5in9tao Tsingtao Ruby 2 11-27-2007 05:19 PM
Explorer.exe error report every time access every type of video me =?Utf-8?B?SXJ3YW5zeWFo?= ASP .Net 4 10-30-2007 07:49 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments