regexp match string with word1 and not word2

F

Flyzone

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
 
J

James Stroud

Flyzone said:
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
 
J

James Stroud

Flyzone said:
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
 
S

Steven Bethard

Flyzone said:
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
 
F

Flyzone

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?
 
F

Flyzone

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.
 
C

Carsten Haese

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
 
M

Marc 'BlackJack' Rintsch

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
 
S

Steven Bethard

Flyzone said:
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
 
M

MRAB

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
 
F

Flyzone

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.
 
A

Ant

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

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