word search

Aloysio Figueiredo xpythonist at yahoo.com.br
Tue Mar 2 18:51:38 EST 2004


I think you will have to use regular expressions. Fortunately, in 
your particular case the regular expression is not a very complicated
one...

=======================================
import re

p = re.compile('.*[^a-zA-Z0-9]cow[^a-zA-Z0-9].*', re.IGNORECASE)

test = ' cow ', '_cow_', '(cow)',' -cow, ', '_cOw-', 'coward'

for t in test:
  if p.match(t):
    print '"cow" is in "%s"' % t
  else:
    print '"cow" is _NOT_ in "%s"' % t

=======================================

result:

"cow" is in " cow "
"cow" is in "_cow_"
"cow" is in "(cow)"
"cow" is in " -cow, "
"cow" is in "_cOw-"
"cow" is _NOT_ in "coward"

You might want to read http://www.amk.ca/python/howto/regex/

Aloysio

 --- Andrea Cova <amcova at fastwebnet.it> escreveu: 
> Hi,
> i'm trying to write a program that finds single words that works like
> this:
> i'd like to find a word, the exact word, without making mistakes
> like, when 
> i'm looking for 'cow', he tells me he found it in 'coward'.
> I have an idea how to do this in C, but i guess it would be less
> complicate 
> in python.
> The findall() function would be ok, but i need to make some
> modifications 
> like this one:
> I want the program to find matches only when the word isn't in the
> middle 
> of other letters. Other kinds of chars instead are accepted. (for
> instance: 
> ' cow ', '_cow_', '(cow)',' -cow, ' are right matches).
> 
> 
> I need this for a program that does the following: i've got a list of
> song 
> titles, saved in a txt file for example, and a list of filenames in a
> 
> directory (mp3 files), and for each song title i want to find the
> best 
> match in the filenames.
> Then when all the matches are done I modify the mp3 files' tags
> according 
> to the information in the txt file.
> I know there are windows programs to do this, but they don't have
> functions 
> like taking a text list and find the corresponding mp3 on their own.
> 
> And two little questions:
> 
> 1-i read in the manuals that if i want "search()" to ignore the case,
> i 
> have to put the flag I. I can't get this to work. Could you write me
> an 
> example of how to write this? I
> 2-how can i make also "findall()" be case UNsensitive?
> 
> Thank you.
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list 

______________________________________________________________________

Yahoo! Mail - O melhor e-mail do Brasil! Abra sua conta agora:
http://br.yahoo.com/info/mail.html




More information about the Python-list mailing list