pattern search
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Mar 27 18:09:12 EDT 2007
En Tue, 27 Mar 2007 18:42:15 -0300, Diez B. Roggisch <deets at nospam.web.de>
escribió:
> Paul McGuire schrieb:
>> On Mar 27, 10:18 am, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
>>> Fabian Braennstroem wrote:
>>>> while iter:
>>>> value = model.get_value(iter, 1)
>>>> if value.endswith("."+ pattern): [...]
>>>>
>>>> Now, I would like to improve it by searching for different 'real'
>>>> patterns just like using 'ls' in bash. E.g. the entry
>>>> 'car*.pdf' should select all pdf files with a beginning 'car'.
>>>> Does anyone have an idea, how to do it?
>>> Use regular expressions. They are part of the module "re". And if you
>>> use them, ditch your code above, and make it just search for a pattern
>>> all the time. Because the above is just the case of
>>> *.ext
>> The glob module is a more direct tool based on the OP's example. The
>> example he gives works directly with glob. To use re, you'd have to
>> convert to something like "car.*\.pdf", yes?
> I'm aware of the glob-module. But it only works on files. I was under
> the impression that he already has a list of files he wants to filter
> instead of getting it fresh from the filesystem.
In that case the best way would be to use the fnmatch module - it already
knows how to translate from car*.pdf into the right regexp. (The glob
module is like a combo os.listdir+fnmatch.filter)
--
Gabriel Genellina
More information about the Python-list
mailing list