Regular Expression

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Regular Expression

Jignesh Sutar
Hi,
 
Why does the follow regular expression not work? I'm trying to pick up all data files within the temp folder which beginwith the file name "Data_W" followed by 3 digits and 3 digits only and nothing more or less.
 
As I understand \d is to indicate any digit from 0-9 and {n} is n occurrences?
 
begin program.
import spss, glob
allfiles = glob.glob("C:/Temp/Data_W\d{3}.sav")
print "\n".join(allfiles),
end program.
 
 
The below works but I want a more stringent criteria.
 
begin program.
import spss, glob
allfiles = glob.glob("C:/Temp/Data_W*.sav")
print "\n".join(allfiles),
end program.
 
Thank in advance
Jignesh
Reply | Threaded
Open this post in threaded view
|

Re: Regular Expression

Jon K Peck
There is a difference between regular expressions and file wildcard expressions, although they look superficially alike.
File wildcards only support *, ? and character ranges via [ ].  This is the construct the operating system uses when filtering files.

glob.glob uses file wildcards, not regular expressions.

So to do what you want, start with the general wildcard expression, say,

glob.glob("C:/Temp/Data_W*.sav")

Then iterate through that list testing with re.match and the regular expression you really want.

HTH,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        J Sutar <[hidden email]>
To:        [hidden email]
Date:        08/03/2012 01:42 PM
Subject:        [SPSSX-L] Regular Expression
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi,
 
Why does the follow regular expression not work? I'm trying to pick up all data files within the temp folder which beginwith the file name "Data_W" followed by 3 digits and 3 digits only and nothing more or less.
 
As I understand \d is to indicate any digit from 0-9 and {n} is n occurrences?
 
begin program.
import spss, glob
allfiles = glob.glob("C:/Temp/Data_W\d{3}.sav")
print "\n".join(allfiles),
end program.

 
 
The below works but I want a more stringent criteria.
 
begin program.
import spss, glob
allfiles = glob.glob("C:/Temp/Data_W*.sav")
print "\n".join(allfiles),
end program.

 
Thank in advance
Jignesh