Use variable in regular expression

Steve Holden steve at
Thu Aug 2 15:32:43 CEST 2007

[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.

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd 
Skype: holdenweb
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

More information about the Python-list mailing list