By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

regexp match string with word1 and not word2

P: n/a
Hello,
i have again problem with regexp :-P
I need to match all lines that contain one word but not contain
another.
Like to do "grep one | grep -v two:"
The syntax of the string is:
(any printable char)two:(any printable char)one(any printable char)
Example:
Apr 30 00:00:09 v890neg0 two: [ID 702911 daemon.one] findings:
blablabla

I tried with:
..*[^t][^w][^o].*one.*
but is not working, the string is always match and in other tries
using "less logic" i get always some different match :-(

P.S: i can't have more re.search, so i just need ONE regexp

Apr 30 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Flyzone wrote:
Hello,
i have again problem with regexp :-P
I need to match all lines that contain one word but not contain
another.
Like to do "grep one | grep -v two:"
The syntax of the string is:
(any printable char)two:(any printable char)one(any printable char)
Example:
Apr 30 00:00:09 v890neg0 two: [ID 702911 daemon.one] findings:
blablabla

I tried with:
.*[^t][^w][^o].*one.*
but is not working, the string is always match and in other tries
using "less logic" i get always some different match :-(

P.S: i can't have more re.search, so i just need ONE regexp
The P.S: suggests homework, but this can't be homework because python
regex won't do this, so your teacher gets an F if its homework. You
require a negative look-behind assertion of variable length--not
possible in python regex. You will have to use something else--or maybe
you don't understand the homework problem.

James
Apr 30 '07 #2

P: n/a
Flyzone wrote:
P.S: i can't have more re.search, so [clip]

This reminds me of a quote by the Great Researcher Roy Garcia:
If it worked the first time, they'd just call it "search".
James
Apr 30 '07 #3

P: n/a
Flyzone wrote:
Hello,
i have again problem with regexp :-P
I need to match all lines that contain one word but not contain
another.
Like to do "grep one | grep -v two:"
You don't need a regexp:;

if 'one' in line and 'two:' not in line:
... do something...

STeVe
Apr 30 '07 #4

P: n/a
James Stroud ha scritto:
The P.S: suggests homework, but this can't be homework because python
regex won't do this, so your teacher gets an F if its homework. You
Not a homework, but a "workwork" :-)
I'm writing a script to parse logfiles, and I have began to study
python for this (bash was too much slow).
When i'll finishi it, I'll post a link here if someone think that
could be helpful.
require a negative look-behind assertion of variable length--not
possible in python regex. You will have to use something else--or maybe
you don't understand the homework problem.
I was asking that cause in the code i wrote this:
for y in range(0, len(skip_lst) ):
if (re.search(skip_lst[y], line)):
skip=1
break

In skip_lst there are the regexp and strings (on one line) to match
with "line".
But rarely the rules to be matched need to have also a logical And.
What could be the solution? Add another array with a "not" regexp?

Apr 30 '07 #5

P: n/a
On 30 Apr, 17:11, Steven Bethard <steven.beth...@gmail.comwrote:
You don't need a regexp:;
I need a regexp.....i'm parsing a file with a rule-file that contains
also regexp and strings too....
Read my post to James Stroud.

Apr 30 '07 #6

P: n/a
On Mon, 2007-04-30 at 08:22 -0700, Flyzone wrote:
On 30 Apr, 17:11, Steven Bethard <steven.beth...@gmail.comwrote:
You don't need a regexp:;

I need a regexp.....i'm parsing a file with a rule-file that contains
also regexp and strings too....
That was not at all evident from your original description. The quality
of the advice you receive is directly influenced by the quality of your
description of the problem you're trying to solve.

-Carsten
Apr 30 '07 #7

P: n/a
In <11*********************@u30g2000hsc.googlegroups. com>, Flyzone wrote:
for y in range(0, len(skip_lst) ):
if (re.search(skip_lst[y], line)):
skip=1
break
Please try to avoid unnecessary indexes::

for regexp in skip_list:
if re.search(regexp, line):
skip = True
break

And if you don't intent to count the `skip`\s a `True` seems to be more
readable.

Ciao,
Marc 'BlackJack' Rintsch
Apr 30 '07 #8

P: n/a
Flyzone wrote:
On 30 Apr, 17:11, Steven Bethard <steven.beth...@gmail.comwrote:
>You don't need a regexp:;

I need a regexp.....i'm parsing a file with a rule-file that contains
also regexp and strings too....
Well then it seems like you might want to rethink this rule-file
approach since your problem is clearly not amenable to regular expressions.

That said, here's a regexp that might work::

((?!two:).)*one((?!two:).)*

That makes a negative lookahead assertion at each character in the string.

STeVe
Apr 30 '07 #9

P: n/a
On Apr 30, 6:49 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1177946458.620210.68...@u30g2000hsc.googlegroups. com>, Flyzone wrote:
for y in range(0, len(skip_lst) ):
if (re.search(skip_lst[y], line)):
skip=1
break

Please try to avoid unnecessary indexes::

for regexp in skip_list:
if re.search(regexp, line):
skip = True
break

And if you don't intent to count the `skip`\s a `True` seems to be more
readable.
Also try to avoid compiling the same regex repeatedly:

compiled_skip_list = [re.compile(regexp) for regexp in skip_list]
...
for regexp in compiled_skip_list:
if regexp.search(line):
skip = True
break

Apr 30 '07 #10

P: n/a
On 30 Apr, 20:00, Steven Bethard <steven.beth...@gmail.comwrote:
Well then it seems like you might want to rethink this rule-file
approach since your problem is clearly not amenable to regular expressions.
[cut]
That said, here's a regexp that might work::
((?!two:).)*one((?!two:).)*
That makes a negative lookahead assertion at each character in the string.
But match again something so don't work like i wanted.

Maybe a right approach will be another if after the first one? Like:
for y in range(0, len(skip_lst) ):
if (re.search(skip_lst[y], line)):
if
(re.search(skip_lst_negative[y], line)):
skip=1
break
and in the rule-file, i could add a new column and check to do the if
just if the second column is not empty.

May 2 '07 #11

P: n/a
Ant
On May 2, 9:17 am, Flyzone <flyz...@technologist.comwrote:
On 30 Apr, 20:00, Steven Bethard <steven.beth...@gmail.comwrote:
....
Maybe a right approach will be another if after the first one? Like:
for y in range(0, len(skip_lst) ):
if (re.search(skip_lst[y], line)):
if
(re.search(skip_lst_negative[y], line)):
skip=1
break
and in the rule-file, i could add a new column and check to do the if
just if the second column is not empty.
This is quite a common approach for this sort of matching problem -
having includes and excludes patterns, and having the test return True
if the include matches but not the exclude. Trying to roll the two
into one requires pretty unreadable regexes if it is possible at all...

May 2 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.