473,396 Members | 1,879 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,396 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 2893
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.