By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Use variable in regular expression

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

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

P: n/a
<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

P: n/a
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

P: n/a
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

P: n/a
[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.