472,961 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

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

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 -> sy******@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 -> sy******@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 -> sy******@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
Jul 19 '05 #1
5 2219
shouldn't it be ".*$search2?" rather than "?.*$search2" ?
I'm creating my regex like this:
$compiled_regex = qr/^(?!.*$search2)$search1(?!.*$search2)/;


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 4:14pm up 5:47 1 user 1.01 1.00
Jul 19 '05 #2
OK, I was being stupid, and really not thinking about what my regex
was actually doing.
I've now solved the problem - for those of you who are interested,
this appears to work:

$compiled_regex = qr/^(?!.*$search2).*$search1/;

J
ja*@hungover.org (James Dyer) wrote in message news:<39*************************@posting.google.c om>...
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 -> sy******@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 -> sy******@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 -> sy******@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

Jul 19 '05 #3
> I've now solved the problem - for those of you who are interested,
this appears to work:
$compiled_regex = qr/^(?!.*$search2).*$search1/;


what's the meaning of "?!" in the regex?

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 7:42pm up 9:15 1 user 0.97 0.93
Jul 19 '05 #4
>> I've now solved the problem - for those of you who are interested,
this appears to work:
$compiled_regex = qr/^(?!.*$search2).*$search1/;


what's the meaning of "?!" in the regex?


I figured it out. need to force the context of the $! variable.

print int($!) . $!;

int($i) prints the error number, 2nd $! prints the message.
--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 7:54pm up 9:27 1 user 1.00 0.94
Jul 19 '05 #5
ja*@hungover.org (James Dyer) wrote in message news:<39*************************@posting.google.c om>...
$compiled_regex = qr/^(?!.*$search2)$search1(?!.*$search2)/;
Ignoring the possiblity that $search1 maches a newline, the second
(?!.*$search2) is redundant. It can never fail to match since the re
engine wouldn't get as that far if there was a match for $search2
anywhere in the data.

$compiled_regex = qr/^(?!.*$search2)$search1/s;
2004-02-18 04:06:50 1AtIua-0001Hh-00 -> sy******@foobar.com R=lookuphost T=remot If $search1 is set to 'sysadmin', and search2 is set to '0008Gs',
You are only looking for $search1 at the start of the string. You
probably wanted.

$compiled_regex = qr/^(?!.*$search2).*$search1/s;

Note - using a single regex for this is probably not a good idea
unless you are forced into doing so by the fact that you are calling
an existing function that you can't modify and that takes a single
regex as an argument.

If you are not compelled to use a single regex it is clearer, and
probably faster to use two.

/$search1/ && !/$search2/
Any thoughts?


Well since you ask...

This topic has been frequently discussed in the Perl newsgroups that
exist on Usenet. I think you should have done a search before you
posted. Having decided you wanted to post I think you should have
done so to a newsgroup that still exists. This one doesn't (see FAQ)
so very few people will see what you post here.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ray Tayek | last post by:
hi, trying to use an xslt to make an xslt. trying something like: <?xml version="1.0" encoding="UTF-8"?> <?xmlspysamplexml H:\java\projects\spy1\spy\inputDocumentMap.xml?> <xsl:stylesheet...
5
by: MyndPhlyp | last post by:
I've been busting my head trying to figure this out for quite some time. With IE6 and NS7, no problems. I can simply code the HTML <img height="100%"> and be done with it. But NS4 and NS6 (and...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
8
by: Greenhorn | last post by:
Hi, Those relational operators have operands of all numerical type int,char,float etc. They also are working for character arrays. Whats the logic behind their working. Is the length of the...
3
by: z. f. | last post by:
Hi, i'm using code in my aspx page. i have data binding where i use <%# Container.DataItem("DateStart") %> i also use code that makes a loop inside a regular <% %> block how can i pass...
8
by: active | last post by:
I use quickwatch on (astrThisOne <> "") and it reports: False as it should because astrThisOne reports: "" Yet If (astrThisOne <> "") Then executes the Then clause
1
by: Robert Dodier | last post by:
Hello, Sorry for asking what must be a FAQ, but I wasn't able to find the answer. I have an XML document fragment which I want to store as a text string. I want a function to convert any XML...
4
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
3
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.