472,960 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

regex problem with re and fnmatch

Hi,

I would like to use re to search for lines in a files with
the word "README_x.org", where x is any number.
E.g. the structure would look like this:
[[file:~/pfm_v99/README_1.org]]

I tried to use these kind of matchings:
# org_files='.*README\_1.org]]'
org_files='.*README\_*.org]]'
if re.match(org_files,line):

Unfortunately, it matches all entries with "README.org", but
not the wanted number!?

After some splitting and replacing I am able to check, if
the above file exists. If it does not, I start to search for
it using the 'walk' procedure:

for root, dirs, files in
os.walk("/home/fab/org"):
for name in dirs:
dirs=os.path.join(root, name) + '/'
for name in files:
files=os.path.join(root, name)
if fnmatch.fnmatch(str(files), "README*"):
print "File Found"
print str(files)
break

As soon as it finds the file, it should stop the searching
process; but there is the same matching problem like above.
Does anyone have any suggestions about the regex problem?
Greetings!
Fabian

Nov 20 '07 #1
2 2873
On Nov 21, 8:05 am, Fabian Braennstroem <f.braennstr...@gmx.dewrote:
Hi,

I would like to use re to search for lines in a files with
the word "README_x.org", where x is any number.
E.g. the structure would look like this:
[[file:~/pfm_v99/README_1.org]]

I tried to use these kind of matchings:
# org_files='.*README\_1.org]]'
org_files='.*README\_*.org]]'
if re.match(org_files,line):
First tip is to drop the leading '.*' and use search() instead of
match(). The second tip is to use raw strings always for your
patterns.
>
Unfortunately, it matches all entries with "README.org", but
not the wanted number!?
\_* matches 0 or more occurrences of _ (the \ is redundant). You need
to specify one or more digits -- use \d+ or [0-9]+

The . in .org matches ANY character except a newline. You need to
escape it with a \.
>>pat = r'README_\d+\.org'
re.search(pat, 'xxxxREADME.org')
re.search(pat, 'xxxxREADME_.org')
re.search(pat, 'xxxxREADME_1.org')
<_sre.SRE_Match object at 0x00B899C0>
>>re.search(pat, 'xxxxREADME_9999.org')
<_sre.SRE_Match object at 0x00B899F8>
>>re.search(pat, 'xxxxREADME_9999Zorg')
>
After some splitting and replacing I am able to check, if
the above file exists. If it does not, I start to search for
it using the 'walk' procedure:
I presume that you mean something like: """.. check if the above file
exists in some directory. If it does not, I start to search for it
somewhere else ..."""
>
for root, dirs, files in
os.walk("/home/fab/org"):
for name in dirs:
dirs=os.path.join(root, name) + '/'
The above looks rather suspicious ...
for thing in container:
container = something_else
????
What are you trying to do?

for name in files:
files=os.path.join(root, name)
and again ....
if fnmatch.fnmatch(str(files), "README*"):
Why str(name) ?
print "File Found"
print str(files)
break

fnmatch is not as capable as re; in particular it can't express "one
or more digits". To search a directory tree for the first file whose
name matches a pattern, you need something like this:
def find_one(top, pat):
for root, dirs, files in os.walk(top):
for fname in files:
if re.match(pat + '$', fname):
return os.path.join(root, fname)

As soon as it finds the file,
"the" file or "a" file???

Ummm ... aren't you trying to locate a file whose EXACT name you found
in the first exercise??

def find_it(top, required):
for root, dirs, files in os.walk(top):
if required in files:
return os.path.join(root, required)

it should stop the searching
process; but there is the same matching problem like above.
HTH,
John
Nov 20 '07 #2
Hi John,

John Machin schrieb am 11/20/2007 09:40 PM:
On Nov 21, 8:05 am, Fabian Braennstroem <f.braennstr...@gmx.dewrote:
>Hi,

I would like to use re to search for lines in a files with
the word "README_x.org", where x is any number.
E.g. the structure would look like this:
[[file:~/pfm_v99/README_1.org]]

I tried to use these kind of matchings:
# org_files='.*README\_1.org]]'
org_files='.*README\_*.org]]'
if re.match(org_files,line):

First tip is to drop the leading '.*' and use search() instead of
match(). The second tip is to use raw strings always for your
patterns.
>Unfortunately, it matches all entries with "README.org", but
not the wanted number!?

\_* matches 0 or more occurrences of _ (the \ is redundant). You need
to specify one or more digits -- use \d+ or [0-9]+

The . in .org matches ANY character except a newline. You need to
escape it with a \.
>>>pat = r'README_\d+\.org'
re.search(pat, 'xxxxREADME.org')
re.search(pat, 'xxxxREADME_.org')
re.search(pat, 'xxxxREADME_1.org')
<_sre.SRE_Match object at 0x00B899C0>
>>>re.search(pat, 'xxxxREADME_9999.org')
<_sre.SRE_Match object at 0x00B899F8>
>>>re.search(pat, 'xxxxREADME_9999Zorg')
Thanks a lot, works really nice!
>After some splitting and replacing I am able to check, if
the above file exists. If it does not, I start to search for
it using the 'walk' procedure:

I presume that you mean something like: """.. check if the above file
exists in some directory. If it does not, I start to search for it
somewhere else ..."""
> for root, dirs, files in
os.walk("/home/fab/org"):
> for name in dirs:
dirs=os.path.join(root, name) + '/'

The above looks rather suspicious ...
for thing in container:
container = something_else
????
What are you trying to do?

> for name in files:
files=os.path.join(root, name)

and again ....
> if fnmatch.fnmatch(str(files), "README*"):

Why str(name) ?
> print "File Found"
print str(files)
break


fnmatch is not as capable as re; in particular it can't express "one
or more digits". To search a directory tree for the first file whose
name matches a pattern, you need something like this:
def find_one(top, pat):
for root, dirs, files in os.walk(top):
for fname in files:
if re.match(pat + '$', fname):
return os.path.join(root, fname)

>As soon as it finds the file,

"the" file or "a" file???

Ummm ... aren't you trying to locate a file whose EXACT name you found
in the first exercise??

def find_it(top, required):
for root, dirs, files in os.walk(top):
if required in files:
return os.path.join(root, required)
Great :-) Thanks a lot for your help... it can be so easy :-)
Fabian
Nov 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Ruby Tuesdays | last post by:
in win32 installation... is it compiled with it or this is a missing features? Thanks
1
by: andrea.gavana | last post by:
Hello NG, I'm quite new to Python and I don't know if this is a FAQ (I can't find it) or an obvious question. I'm using the RE module in python, and I would like to be able to contruct something...
5
by: Thomas Rademacher | last post by:
Hello, I want to collect with the wildcard '*' all existing directories. For example: /dir/dir/*/dir/*/dir/* or C:\dir\dir\*\dir\*\dir\* How can I resolve this problem? Thanks for your...
1
by: Graeme Downes | last post by:
Hi I'm trying to create a regular expression for C using the fnmatch function from the fnmatch.h library. I dont know if you can help, but i need something as follows: #:]# where #...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned... Can someone explain please? for root, dirs,...
3
by: affiliateian | last post by:
I am pretty new to php. Can anyone tell me what the proper syntax is for checking a particular domain from a web form? For instance, I am looking for an if statement that accomplishes this: if...
4
by: abcd | last post by:
I am using fnmatch.fnmatch to find some files. The only problem I have is that it only takes one pattern...so if I want to search using multiple patterns I have to do something like.... ...
5
by: Mr.SpOOn | last post by:
Hi, is there any way to search elements in a list using wildcards? I have a list of various elements and I need to search for elements starting with 'no', extract them and put in a new list. I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.