text matching help
Tim Williams
listserver at tdw.net
Thu Sep 9 09:01:41 EDT 2004
----- Original Message -----
From: "Heiko Wundram" <heikowu at ceosg.de>
> Am Donnerstag, 9. September 2004 13:28 schrieb Tim Williams:
> > I need to be able to match some input text to these wildcards (eg
timothy
> > will match tim*), * on its own is invalid, there must be text before
or
> > after the *.
>
> Check out the fnmatch module (if you use simple wildcards, unix shell
style,
> which aren't regular expressions):
>
> >>> import fnmatch
> >>> fnmatch.fnmatch("mylittletext","my*")
> True
> >>> fnmatch.fnmatch("mylittletext","little*")
> False
> >>> fnmatch.fnmatch("mylittletext","*little*")
> True
Yes, this does the trick, many thanks.
I used something like
import fnmatch as f
t = 'tim.williams'
# in place of L = MyDict.keys()
L = ['tim*' , 'dav* ', 'david' , 'peter' , '*m' , '*y' , ' not-a-name ',
'timoth?' , '*.*' , '*iams' , '*']
L.sort() # to give a consistent search order
for name in L:
... if f.fnmatch(t, name):
... print t, 'matches', name
tim.williams matches *
tim.williams matches *.*
tim.williams matches *iams
tim.williams matches tim*
:-)
More information about the Python-list
mailing list