473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

regexp match string with word1 and not word2

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
11 3534
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maxime Biais | last post by:
Hello, I heard that is possible to replace %(word) in a string with a dictionary containing { "word" : "replaced word" } with a function or a builtin from the python library. For example with...
2
by: Joshua Forgione | last post by:
Hello. I am reverse engineering some Python Code. I am using the book "The Quick Python Book" as an aide. The following line exists in the code: msg = struct.pack('BBBB',word0, word1, word2,...
6
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
4
by: Ross | last post by:
Anyone have some code to help me understand how I can convert a given hex string, say "0009dbaa00004c00000000fb82ca621c," into a struct of this form: struct uniqueid { ulong32_t word1;...
9
by: Nathan Sokalski | last post by:
I am trying to do a database search using LIKE using the following code: Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click If...
11
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines...
12
by: JackYee123 | last post by:
Hey, I need a structure to store a string array in c, for example Index Content -------- ----------- 0 word1 1 word2 2 3
5
by: NJonge01 | last post by:
Hi, I haven't even tried to code this yet, but I need to create a "search" string that would work like this: Read this: strText = Create this string StrCriteria = Like "*Word1*" and Like...
9
by: jl_post | last post by:
Hi, A few months back I remember reading through C++ newsgroups trying to find a way to quickly convert a number to a C++ std::string. I often see code like: // Create a string that holds a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.