[Tutor] Regexp Not Matching on Numbers?

Kent Johnson kent37 at tds.net
Tue Dec 14 18:41:03 CET 2004


I'm not too sure what you are trying to do here, but the re in your code matches the names in your 
example data:
 >>> import re
 >>> name = 'partners80_access_log.1102723200'
 >>> re.compile(r"([\w]+)").match( name ).groups()
('partners80_access_log',)

One thing that may be tripping you up is that re.match() only matches at the *start* of a string, as 
if the regex starts with '^'. For matching anywhere in the string use re.search() instead.

Another possible problem is that you are defining regexp but not using it - have you been trying to 
change the value of regexp and wondering why the program doesn't change?

If you are looking for file names that end in log.ddd then try
re.search(r'log\.\d+$', name)

If this doesn't help please be more specific about the format of the names you want to match and 
exclude.

Kent

Gooch, John wrote:
> This is weird. I have a script that checks walks through directories, checks
> to see if their name matches a certain format ( regular expression ), and
> then prints out what it finds. However, it refuses to ever match on numbers
> unless the regexp is ".*". So far I have tried the following regular
> expressions:
> "\d+"
> "\d*"
> "\W+"
> "\W*"
> "[1-9]+"
> and more...
> 
> 
> Here is an example of the output:
> No Match on partners80_access_log.1102723200
> Checking on type=<type 'str'> Name = some_access_log.1102896000
> 
> 
> Here is the code:
> import os,sys
> import re
> #define the file mask to identify web log files
> regexp = re.compile( r"\." )
> 
> startdir = "C:/Documents and Settings/John.Gooch/My Documents/chaser/"
> #define global functions
> def delFile(arg, dirname, names):
>     found = 0
>     for name in names:
>         print "Checking on type="+str(type( name ) )+" Name = "+str(name)
>         matches = re.compile(r"([\w]+)").match( name )
>         if matches:
>             print "Match on "+str(matches.groups()) 
>             found = 1
>         else:
>             print "No Match on "+name
>     if not found:
>         print "No matches found in "+dirname
>     else:
>         print "Match found in "+dirname
> os.path.walk( startdir, delFile, "" )
> 
> 
> 
> 
> Any thoughts?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list