Use variable in regular expression

Discussion in 'Python' started by CarpeSkium@gmail.com, Aug 2, 2007.

  1. Guest

    I know I can use a variable in regular expressions. I want to use a
    regex to find something based on the beginning of the string. I am
    using yesterday's date to find all of my data from yesterday.
    Yesterday's date is 20070731, and assigned to the variable
    "yesterday_date". I want to loop thru a directory and find all of the
    yesterday's data ONLY IF the feature class has the date at the
    BEGINNING of the filename.

    Sample strings:
    20070731_test1
    Copy20070731_test1
    20070731_test2
    Copy20070731_test2
    20070731_test3
    Copy20070731_test3

    I don't want the one's that start with "Copy". I can't figure out the
    syntax of inserting the "^" into the regex. I've tried all of the
    following, with no luck:

    re.compile(^yesterday_date)
    re.compile(r'^yesterday_date')
    re.compile(r'^[yesterday_date]')
    re.compile(r'[^yesterday_date]')

    I don't know what I'm doing and I'm just guessing at this point. Can
    anyone help? Thanks.

    Mark
     
    , Aug 2, 2007
    #1
    1. Advertising

  2. On 2007-08-02, at 13:43, wrote:

    > I know I can use a variable in regular expressions. I want to use a
    > regex to find something based on the beginning of the string. I am
    > using yesterday's date to find all of my data from yesterday.
    > Yesterday's date is 20070731, and assigned to the variable
    > "yesterday_date". I want to loop thru a directory and find all of the
    > yesterday's data ONLY IF the feature class has the date at the
    > BEGINNING of the filename.
    >


    > ...


    > I don't want the one's that start with "Copy". I can't figure out the
    > syntax of inserting the "^" into the regex. I've tried all of the
    > following, with no luck:
    >
    > re.compile(^yesterday_date)
    > re.compile(r'^yesterday_date')
    > re.compile(r'^[yesterday_date]')
    > re.compile(r'[^yesterday_date]')


    The first one is a syntax error (^ outside a string means the xor-
    operation). The rest are just strings containing the _string_
    'yesterday_date' and not the value of the variable. So you need to do
    some string formatting(*

    search_str = '^%s' % yesterday_date # I'm assuming yesterday_date is
    a string.
    re.compile(search_str)

    *) http://docs.python.org/lib/typesseq-strings.html

    --
    [ <*> Antti Rasinen ]

    This drone-vessel speaks with the voice and authority of the Ur-Quan.
     
    Antti Rasinen, Aug 2, 2007
    #2
    1. Advertising

  3. <> wrote:
    >I know I can use a variable in regular expressions. I want to use a
    >regex to find something based on the beginning of the string.


    You're coming from a Perl background, right? No-one else would
    think of using a regexp for such a simple thing. There are two
    things you need to learn:

    (a) Python doesn't do automatic variable interpolation in strings.
    (b) For simple find and replace operations, there are string
    methods which are easier and faster than regexps.

    >Yesterday's date is 20070731, and assigned to the variable
    >"yesterday_date". I want to loop thru a directory and find all of the
    >yesterday's data ONLY IF the feature class has the date at the
    >BEGINNING of the filename.
    >
    >Sample strings:
    >20070731_test1
    >Copy20070731_test1
    >20070731_test2
    >Copy20070731_test2
    >20070731_test3
    >Copy20070731_test3
    >
    >I don't want the one's that start with "Copy".


    >>> "20070731_test1".startswith(yesterday_date)

    True
    >>> "Copy20070731_test1".startswith(yesterday_date)

    False

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
     
    Sion Arrowsmith, Aug 2, 2007
    #3
  4. Steve Holden Guest

    wrote:
    > I know I can use a variable in regular expressions. I want to use a
    > regex to find something based on the beginning of the string. I am
    > using yesterday's date to find all of my data from yesterday.
    > Yesterday's date is 20070731, and assigned to the variable
    > "yesterday_date". I want to loop thru a directory and find all of the
    > yesterday's data ONLY IF the feature class has the date at the
    > BEGINNING of the filename.
    >
    > Sample strings:
    > 20070731_test1
    > Copy20070731_test1
    > 20070731_test2
    > Copy20070731_test2
    > 20070731_test3
    > Copy20070731_test3
    >
    > I don't want the one's that start with "Copy". I can't figure out the
    > syntax of inserting the "^" into the regex. I've tried all of the
    > following, with no luck:
    >
    > re.compile(^yesterday_date)
    > re.compile(r'^yesterday_date')
    > re.compile(r'^[yesterday_date]')
    > re.compile(r'[^yesterday_date]')
    >
    > I don't know what I'm doing and I'm just guessing at this point. Can
    > anyone help? Thanks.
    >

    As is often the case, taking a larger look at the problem can reveal
    that Python has features that can help you without even getting down to
    more complex stuff.

    You appear to require a list of the files whose names begin with a
    string representation of yesterday's date.

    If you take a look at the glob module you will see that it has a glob()
    function, and if you were to call it as

    names = glob.glob(yesterday_date + "*")

    it would return a list of the names of the files you are interested in.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Aug 2, 2007
    #4
  5. Steve Holden Guest

    wrote:
    > I know I can use a variable in regular expressions. I want to use a
    > regex to find something based on the beginning of the string. I am
    > using yesterday's date to find all of my data from yesterday.
    > Yesterday's date is 20070731, and assigned to the variable
    > "yesterday_date". I want to loop thru a directory and find all of the
    > yesterday's data ONLY IF the feature class has the date at the
    > BEGINNING of the filename.
    >
    > Sample strings:
    > 20070731_test1
    > Copy20070731_test1
    > 20070731_test2
    > Copy20070731_test2
    > 20070731_test3
    > Copy20070731_test3
    >
    > I don't want the one's that start with "Copy". I can't figure out the
    > syntax of inserting the "^" into the regex. I've tried all of the
    > following, with no luck:
    >
    > re.compile(^yesterday_date)
    > re.compile(r'^yesterday_date')
    > re.compile(r'^[yesterday_date]')
    > re.compile(r'[^yesterday_date]')
    >
    > I don't know what I'm doing and I'm just guessing at this point. Can
    > anyone help? Thanks.
    >

    As is often the case, taking a larger look at the problem can reveal
    that Python has features that can help you without even getting down to
    more complex stuff.

    You appear to require a list of the files whose names begin with a
    string representation of yesterday's date.

    If you take a look at the glob module you will see that it has a glob()
    function, and if you were to call it as

    names = glob.glob(yesterday_date + "*")

    it would return a list of the names of the files you are interested in.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Aug 2, 2007
    #5
  6. Steve Holden Guest

    [when replying to a mailing list or newsgroup response please make sure
    you include the list as a recipient, so the whole conversation is available]

    André Martins wrote:
    >
    >> I know I can use a variable in regular expressions. I want to use a
    >> regex to find something based on the beginning of the string. I am
    >> using yesterday's date to find all of my data from yesterday.
    >> Yesterday's date is 20070731, and assigned to the variable
    >> "yesterday_date". I want to loop thru a directory and find all of the
    >> yesterday's data ONLY IF the feature class has the date at the
    >> BEGINNING of the filename.

    >
    > If i understood, you have directores with files named 20070731, 20070722 ...
    >
    > So, what about:
    >
    > import os
    > yesterday_date = '20070731'
    > list = os.listdir (dir)
    > for x in [x for x in list if x.startswith( yesterday_date ) ]:
    > print x
    >
    >
    > Is not a option?
    >

    If it works it's an option! Regular expressions should usually be a last
    resort, and the solution above seems perfectly acceptable to me.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Aug 2, 2007
    #6
    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. Mark Fletcher
    Replies:
    1
    Views:
    506
    Mark Fletcher
    May 19, 2004
  2. VSK
    Replies:
    2
    Views:
    2,392
  3. Replies:
    1
    Views:
    115
    Jeff Pritchard
    Jul 19, 2006
  4. Tony
    Replies:
    2
    Views:
    142
    Tad McClellan
    Apr 21, 2005
  5. SamL
    Replies:
    6
    Views:
    166
    Ted Zlatanov
    Mar 25, 2009
Loading...

Share This Page