Match 2 words in a line of file

Daniel Klein danielkleinad at gmail.com
Fri Jan 19 12:03:33 EST 2007


On 18 Jan 2007 18:54:59 -0800, "Rickard Lindberg"
<ricli576 at student.liu.se> 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 bar is
>a word in that line.
>
>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).

1) Depends how you define a 'word'.

2) This can be resolved with

templine = ' ' + line + ' '
if ' ' + word1 + ' ' in templine and ' ' + word2 + ' ' in templine:


Dan



More information about the Python-list mailing list