[Tutor] Searching for text while ignoring case

Michael Janssen Janssen@rz.uni-frankfurt.de
Tue Apr 1 13:45:06 2003


On Tue, 1 Apr 2003 jlohman@cox.net wrote:

> How do I make a command like FIND() search for text strings in a line
> and can recognize them regardless of case? A word like "puppy" would
> be "Puppy" sometimes if it started a sentence. However, a search for
> "puppy" will miss "Puppy".
>
> How do I make text search case-insensitive? Surely this is a common
> task and there should be a command to deal with it...but I cannot find
> it<g>.

two opportunities:

* lower() both line and searchstring (or upper() - just what you like
better)

* search() from re-modul with flag re.IGNORECASE:

mt = re.search(search_string, line, re.IGNORECASE)
if mt:
    # search string was found
    mt.group()

Michael
>
> -Jeff
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>