[Tutor] regexes, thanks
Matt
matt@ipwib.net
Mon Nov 18 22:39:02 2002
I'm working on a renaming utility for use in renaming mp3's that I
ripped from cd while using Windows (and consequently using my preferred
naming convention at the time, which I no longer wish to use). I
created a file listing, then tried manipulating it. I wanted to make it
change every word (defined by only letter sequences) to have the first
letter changed to uppercase. Only it doesn't seem that regexes are
fully implemented in python (\u,\U)... So, I tried using a function:
def upper(a)
return(string.capitalize(a))
words = re.compile('([a-zA-Z]+)')
new_filename = words.sub(upper, filename)
Only this doesn't work, as it doesn't pass the matched string, but a
MatchObject. This seemed really strange to me. Eventually I came up
with this:
def upper(a):
return(string.capitalize(a.string[a.start():a.end()]))
words = re.compile('([a-zA-Z]+)')
new_filename = words.sub(upper, filename)
This seems like a really complicated way to do something that I thought
would be relatively simple. Is this the best way to do it? Or am I off
track and creating unnecessary complexity?
Btw, thanks to Danny Yoo for helping me out with python audio
information. Several of the sites have proved extremely helpful.
-Matt