pattern search

Diez B. Roggisch deets at nospam.web.de
Tue Mar 27 11:18:03 EDT 2007


Fabian Braennstroem wrote:

> Hi,
> 
> I wrote a small gtk file manager, which works pretty well. Until
> now, I am able to select different file (treeview entries) just by
> extension (done with 'endswith'). See the little part below:
> 
>                 self.pathlist1=[ ]
>                 self.patternlist=[ ]
>                 while iter:
> #                print iter
>                     value = model.get_value(iter, 1)
> #                if value is what I'm looking for:
>                     if value.endswith("."+ pattern):
>                         selection.select_iter(iter)
>                         selection.select_path(n)
>                         self.pathlist1.append(n)
>                         self.patternlist.append(value)
>                     iter = model.iter_next(iter)
> #                print value
>                     n=n+1
> 
> 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



Diez



More information about the Python-list mailing list