473,659 Members | 2,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.o rg", 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='.*RE ADME\_1.org]]'
org_files='.*RE ADME\_*.org]]'
if re.match(org_fi les,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.jo in(root, name) + '/'
for name in files:
files=os.path.j oin(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 2907
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.o rg", 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='.*RE ADME\_1.org]]'
org_files='.*RE ADME\_*.org]]'
if re.match(org_fi les,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+\.o rg'
re.search(pat , 'xxxxREADME.org ')
re.search(pat , 'xxxxREADME_.or g')
re.search(pat , 'xxxxREADME_1.o rg')
<_sre.SRE_Mat ch object at 0x00B899C0>
>>re.search(pat , 'xxxxREADME_999 9.org')
<_sre.SRE_Mat ch object at 0x00B899F8>
>>re.search(pat , 'xxxxREADME_999 9Zorg')
>
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.jo in(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.j oin(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(ro ot, 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(ro ot, 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.o rg", 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='.*RE ADME\_1.org]]'
org_files='.*RE ADME\_*.org]]'
if re.match(org_fi les,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.
>Unfortunatel y, 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+\.o rg'
re.search(pa t, 'xxxxREADME.org ')
re.search(pa t, 'xxxxREADME_.or g')
re.search(pa t, 'xxxxREADME_1.o rg')
<_sre.SRE_Mat ch object at 0x00B899C0>
>>>re.search(pa t, 'xxxxREADME_999 9.org')
<_sre.SRE_Mat ch object at 0x00B899F8>
>>>re.search(pa t, 'xxxxREADME_999 9Zorg')
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.jo in(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.j oin(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(ro ot, 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(ro ot, 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
2765
by: Ruby Tuesdays | last post by:
in win32 installation... is it compiled with it or this is a missing features? Thanks
1
1170
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 like the Window$ "Find Files Or Folders" engine. As the Window$ users know, you can filter the file names using the character "*", as: myfilen* (Will find a file named myfilenames) *yf*nam* (Will find a file...
5
1819
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 hints, Thomas.
1
2741
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 # represents any number with any number of digits
17
3961
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 http://forta.com/books/0672325667/
7
4544
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, files in os.walk('/directory/'): print root # print dirs # print files
3
1299
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 ($domain=*.mydomain.com) For some reason, can't get the proper syntax so that sciprt will take. Thoughts?
4
18762
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.... patterns = for p in patterns: if fnmatch.fnmatch(some_file_name, p): return True
5
18688
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 was thinking about something like: mylist.index('no*') Of course this doesn't work.
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.