Use variable in regular expression

C

CarpeSkium

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
 
A

Antti Rasinen

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
 
S

Sion Arrowsmith

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".
False
 
S

Steve Holden

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 -------------
 
S

Steve Holden

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 -------------
 
S

Steve Holden

[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 said:
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 -------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top