In Python, is there a better way to parameterise strings into regular expressions than doing it manually like this:
test = 'flobalob'
names = ['a', 'b', 'c']
for name in names:
regexp = "%s" % (name)
print regexp, re.search(regexp, test)
This noddy example tries to match each name in turn. I know there's better ways of doing that, but its a simple example purely to illustrate the point.
The answer appears to be no, there's no real alternative. The best way to paramaterise regular expressions in python is as above or with derivatives such as str.format()
. I tried to write a generic question, rather than 'fix ma codez, kthxbye'. For those still interested, I've fleshed out an example closer to my needs here:
for diskfilename in os.listdir(''):
filenames = ['bob.txt', 'fred.txt', 'paul.txt']
for filename in filenames:
name, ext = filename.split('.')
regexp = "%s.*\.%s" % (name, ext)
m = re.search(regexp, diskfilename)
if m:
print diskfilename, regexp, re.search(regexp, diskfilename)
# ...
I'm trying to figure out the 'type' of a file based on its filename, of the form <filename>_<date>.<extension>
. In my real code, the filenames
array is a dict, containing a function to call once a match is found.
Other ways I've considered doing it:
Have a regular expression in the array. I already have an array of filenames without any regular expression magic, so I am loathe to do this. I have done this elsewhere in my code and its a mess (though necessary there).
Match only on the start of the filename. This would work, but would break with .bak copies of files, etc. At some point I'll probably want to extract the date from the filename so would need to use a regular expression anyway.
Thanks for the responses suggesting alternatives to regular expressions to achieve the same end result. I was more interested in parameterising regular expressions for now and for the future. I never come across fnmatch, so its all useful in the long run.