Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Newbie regular expression ? (http://www.velocityreviews.com/forums/t349872-newbie-regular-expression.html)

len 10-04-2005 06:32 PM

Newbie regular expression ?
 
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

I have read through the documentation and I am now sooooo
confussedddddd!!

Len Sumnler


Fredrik Lundh 10-04-2005 06:44 PM

Re: Newbie regular expression ?
 
"len" <lsumnler@gmail.com> wrote:

>I have the following statement and it works fine;
>
> list1 = glob.glob('*.dat')


that's a glob pattern, not a regular expression.

> however I now have an additional requirement the the string must begin
> with any form of "UNQ,Unq,unq,..."


list1 = glob.glob('*.dat')
list1 = [file for file in list1 if file.lower().startswith("unq")]

</F>




jepler@unpythonic.net 10-04-2005 06:44 PM

Re: Newbie regular expression ?
 
Here are two ideas that come to mind:
files = glob.glob("UNQ*.dat") + glob.glob("Unq*.dat") + glob.glob("unq.dat")

files = [f for f in glob.glob("*.dat") if f[:3] in ("UNQ", "Unq", "unq")]

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDQs2mJd01MZaTXX0RAkPpAKCfKqMCXC/LKLuMDq6KJ4cQjX7EWwCgm2Ho
q+fZjjIzhHsS4l1yMoW3kzE=
=ikw+
-----END PGP SIGNATURE-----


David Murmann 10-04-2005 06:56 PM

Re: Newbie regular expression ?
 
jepler@unpythonic.net wrote:
> Here are two ideas that come to mind:
> files = glob.glob("UNQ*.dat") + glob.glob("Unq*.dat") + glob.glob("unq.dat")
>
> files = [f for f in glob.glob("*.dat") if f[:3] in ("UNQ", "Unq", "unq")]


actually i think he wanted "unq" to be case-insensitive which could be
done with:

files = [f for f in glob.glob("*.dat") if f.lower().startswith("unq")]

David.

Micah Elliott 10-04-2005 07:15 PM

Re: Newbie regular expression ?
 
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>

Micah Elliott 10-04-2005 07:21 PM

Re: Newbie regular expression ?
 
On Oct 04, Micah Elliott wrote:
> $ man 3 fnmatch


Actually "man 7 glob" would be better (assuming you've got *nix). Also
note that globs are not regular expressions. "pydoc glob" is another
reference.

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

Steve Holden 10-04-2005 07:31 PM

Re: Newbie regular expression ?
 
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
>
> I have read through the documentation and I am now sooooo
> confussedddddd!!
>

You don't need regular expressions. You want

list1 = glob.glob("[Uu][Nn][Qq]*.dat")

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


len 10-04-2005 07:39 PM

Re: Newbie regular expression ?
 
Thanks everyone for your help.

I took the option of f1.lower().startswith("unq").

Len Sumnler



All times are GMT. The time now is 07:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.