Newbie regular expression ?

Discussion in 'Python' started by len, Oct 4, 2005.

  1. len

    len Guest

    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
    len, Oct 4, 2005
    #1
    1. Advertising

  2. "len" <> 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>
    Fredrik Lundh, Oct 4, 2005
    #2
    1. Advertising

  3. len

    Guest

    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-----
    , Oct 4, 2005
    #3
  4. 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.
    David Murmann, Oct 4, 2005
    #4
  5. 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, Oct 4, 2005
    #5
  6. 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>
    Micah Elliott, Oct 4, 2005
    #6
  7. len

    Steve Holden Guest

    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/
    Steve Holden, Oct 4, 2005
    #7
  8. len

    len Guest

    Thanks everyone for your help.

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

    Len Sumnler
    len, Oct 4, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,283
  2. Lee
    Replies:
    6
    Views:
    352
    Alan Moore
    Oct 14, 2003
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    839
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,942
    vforvikash
    Dec 29, 2008
  5. googleboy
    Replies:
    6
    Views:
    329
    Paul McGuire
    Sep 23, 2005
Loading...

Share This Page