472,108 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,108 software developers and data experts.

Use variable in regular expression

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 '07 #1
5 9667

On 2007-08-02, at 13:43, Ca********@gmail.com 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

--
[ ar*@iki.fi <*Antti Rasinen ]

This drone-vessel speaks with the voice and authority of the Ur-Quan.

Aug 2 '07 #2
<Ca********@gmail.comwrote:
>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 -- si***@chiark.greenend.org.uk -- 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
Aug 2 '07 #3
Ca********@gmail.com 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 -------------
Aug 2 '07 #4
Ca********@gmail.com 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 -------------

Aug 2 '07 #5
[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 -------------
Aug 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Miguel Orrego | last post: by
1 post views Thread by Susan | last post: by
1 post views Thread by Martin John Brindle | last post: by
4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
6 posts views Thread by OKB (not okblacke) | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.