Use variable in regular expression

CarpeSkium at gmail.com CarpeSkium at gmail.com
Thu Aug 2 06:43:21 EDT 2007


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




More information about the Python-list mailing list