Regex problem, match if line contains <a>, unless it also contains <b>

From: James Dyer (jad_at_hungover.org)
Date: 02/19/04


Date: 19 Feb 2004 05:43:49 -0800

I'm having problems getting a regex to work.
Basically, given two search parameters ($search1 and $search2), it
should allow me to filter a log file such that lines with the $search1
string in are printed, unless the $search2 string is also in that line
somewhere (either before or after $search1).

I'm creating my regex like this:
$compiled_regex = qr/^(?!.*$search2)$search1(?!.*$search2)/;

I then use it:

while( <> ) {
  next if( $_ !~ /$compiled_regex/ );
  print $_ . "\n";
}

With the following test data:

2004-02-18 04:06:50 1AtIua-0001Hh-00 -> sysadmin@foobar.com R=lookuphost T=remot
e_smtp H=mxhost-1.foo.bar [0.0.0.0]
2004-02-19 04:02:02 1AtfNx-0008DC-00 -> sysadmin@foobar.com R=lookuphost T=remot
e_smtp H=mxhost-1.foo.bar [0.0.0.0]
2004-02-19 04:07:26 1AtfO5-0008Gs-00 -> sysadmin@foobar.com R=lookuphost T=remot
e_smtp H=mxhost-1.foo.bar [0.0.0.0]

If $search1 is set to 'sysadmin', and search2 is set to '0008Gs',
none of the lines in the data are displayed, whereas I would expect the
first two to be displayed.

With this test data:
foo
foo foo
foo foo foo
foo bar
bar foo
foo bar foo
foo bar bar
bar foo bar
bar foo foo
bar
bar bar
bar bar bar

$search1 set to 'foo', and $search2 set to 'bar', I get the
expected results (foo, foo foo and foo foo foo displayed).

I just can't figure out why nothing is being displayed in my first test case.
My gut instinct is that it's got something to do with the special'ish
characters in the data ('-', '>' etc.), but I'm not sure.

Any thoughts?

J



Relevant Pages

  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)
  • Re: Magic function
    ... processing objects created in root at depth 3 ... root obj2 at depth 3 ... processing objects created in foo at depth 2 ... processing objects created in bar at depth 0 ...
    (comp.lang.python)
  • Re: from __future__ import absolute_import ?
    ... foo not in bar ... Unfortunately this is a side effect of using the os's directory structure to represent a python "package" structure. ...
    (comp.lang.python)
  • Re: from __future__ import absolute_import ?
    ... foo not in bar ... A path below the package level is generally a good means to shoot ... to represent a python "package" structure. ...
    (comp.lang.python)
  • Re: how to deserialize variable element/node
    ... string bar; ... In the first deserialization case, bar = "sometimes a simple string is ... Your two XML fragments would have to be represented in an XML schema by ...
    (microsoft.public.dotnet.xml)