473,382 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 9741

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
15
by: Miguel Orrego | last post by:
Hi, I have a variable in an app called GenericTitle which contains text, a persons job title funnily enough. I want to check whether this variable contains the word "director" and if it does,...
1
by: Susan | last post by:
Hi all, I have the following variable assignments in a script: var strEmail = document.new_record.email.value; var strJob = document.new_record.occupation.value; var strNumbers =...
1
by: Martin John Brindle | last post by:
I need to build a regular expression where the expression contents are variable. for example I have a string that i need to search for, but the string can change. If i have a variable called...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
6
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that....
6
by: OKB (not okblacke) | last post by:
For years now Python has not supported variable-length lookbehinds. I'm just curious whether there are any plans to change this in Python 3.0, or before, or after. It seems that Perl 6 will allow...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.