Re: Match 2 words in a line of file

2007-01-20 Thread Alex
egc> how do i write a regexp for this.. or better yet shd i even be using egc> regexp or is there a better way to do this "A team of engineers were faced with a problem; they decided to handle it with regular expressions. Now they had two problems" Regular expressions are not always the bes

Re: Match 2 words in a line of file

2007-01-20 Thread Steven D'Aprano
On Fri, 19 Jan 2007 22:57:37 -0800, Rickard Lindberg wrote: > Daniel Klein wrote: > >> 2) This can be resolved with >> >> templine = ' ' + line + ' ' >> if ' ' + word1 + ' ' in templine and ' ' + word2 + ' ' in templine: > > But then you will still have a problem to match the word "foo" in a > s

Re: Match 2 words in a line of file

2007-01-19 Thread Rickard Lindberg
Daniel Klein wrote: > 2) This can be resolved with > > templine = ' ' + line + ' ' > if ' ' + word1 + ' ' in templine and ' ' + word2 + ' ' in templine: But then you will still have a problem to match the word "foo" in a string like "bar (foo)". -- http://mail.python.org/mailman/listinfo/python

Re: Match 2 words in a line of file

2007-01-19 Thread Daniel Klein
On 18 Jan 2007 18:54:59 -0800, "Rickard Lindberg" <[EMAIL PROTECTED]> wrote: >I see two potential problems with the non regex solutions. > >1) Consider a line: "foo (bar)". When you split it you will only get >two strings, as split by default only splits the string on white space >characters. Thus

Re: Match 2 words in a line of file

2007-01-19 Thread bearophileHUGS
Rickard Lindberg, yesterday I was sleepy and my solution was wrong. > 2) If you have a line something like this: "foobar hello" then "'foo' > in line" will return true, even though foo is not a word (it is part of > a word). Right. Now I think the best solution is to use __contains__ (in) to quic

Re: Match 2 words in a line of file

2007-01-19 Thread harvey . thomas
Rickard Lindberg wrote: > I see two potential problems with the non regex solutions. > > 1) Consider a line: "foo (bar)". When you split it you will only get > two strings, as split by default only splits the string on white space > characters. Thus "'bar' in words" will return false, even though

Re: Match 2 words in a line of file

2007-01-19 Thread harvey . thomas
Rickard Lindberg wrote: > I see two potential problems with the non regex solutions. > > 1) Consider a line: "foo (bar)". When you split it you will only get > two strings, as split by default only splits the string on white space > characters. Thus "'bar' in words" will return false, even though

Re: Match 2 words in a line of file

2007-01-18 Thread Rickard Lindberg
I see two potential problems with the non regex solutions. 1) Consider a line: "foo (bar)". When you split it you will only get two strings, as split by default only splits the string on white space characters. Thus "'bar' in words" will return false, even though bar is a word in that line. 2) If

Re: Match 2 words in a line of file

2007-01-18 Thread bearophileHUGS
MrJean1 wrote: > def lines_with_words(file, word1, word2): > """Print all lines in file that have both words in it.""" > for line in file: > words = line.split() > if word1 in words and word2 in words: > print line This sounds better, it's probably faster than t

Re: Match 2 words in a line of file

2007-01-18 Thread MrJean1
Without using re, this may work (untested ;-): def lines_with_words(file, word1, word2): """Print all lines in file that have both words in it.""" for line in file: words = line.split() if word1 in words and word2 in words: print line /Jean Brouwers Rickard

Re: Match 2 words in a line of file

2007-01-18 Thread Rickard Lindberg
[EMAIL PROTECTED] wrote: > 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 that > has the word 'event' and the word 'new'. > > Note that i need lin

Match 2 words in a line of file

2007-01-18 Thread elrondrules
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 that has the word 'event' and the word 'new'. Note that i need lines that has both the words only and not