Newbie regular expression ?

Micah Elliott mde at micah.elliott.name
Tue Oct 4 15:15:05 EDT 2005


On Oct 04, len wrote:
> I have the following statement and it works fine;
> 
>     list1 = glob.glob('*.dat')
> 
> however I now have an additional requirement the the string must begin
> with any form of "UNQ,Unq,unq,..."
> 
> as an example if I had the following four files in the directory:
> 
> unq123abc.dat
> xy4223.dat
> myfile.dat
> UNQxyc123489-24.dat
> 
> only unq123abc.dat and UNQxyc123489-24.dat would be selected

If glob is your preferred means, one option is:

   $ touch unq1.dat UnQ1.dat unQ1.dat UNQ1.dat foo.dat
   $ python -c '
   - import glob
   - print glob.glob("[uU][nN][qQ]*.dat")
   - '
   ['unq1.dat', 'UnQ1.dat', 'unQ1.dat', 'UNQ1.dat']
   $ man 3 fnmatch

-- 
Micah Elliott
<mde at micah dot elliott dot name>



More information about the Python-list mailing list