String Search.

Hans Nowak wurmy at earthlink.net
Fri Nov 9 18:45:35 EST 2001


Adonis Vargas wrote:

> i posted a thread before asking help on how to find a specific string within
> a line of string, i unfortunately did not get too specific on what i was
> asking for. how can i do a search within a string using wildcards?
> i.e.:
> (pseudocode)
> if "*something*is*" in "something is written here": return true
>
> ive looked into the re module and have been to the how-to pages, but i cant
> seem to understand it.

I suppose that, for a simple case like this, you can (ab)use the fnmatch module:

>>> import fnmatch
>>> fnmatch.fnmatch('something is written here', '*something*is*')
1
>>> fnmatch.fnmatch('something was written here', '*something*is*')
0
>>>

For more sophisticated stuff, you'll really need the re module, though (or
something with similar power).

HTH,

--Hans Nowak




More information about the Python-list mailing list