[Tutor] Replacing part of a URL

Steven D'Aprano steve at pearwood.info
Sun Feb 21 01:25:31 CET 2010

On Sun, 21 Feb 2010 09:34:34 am Lao Mao wrote:
> Hello,
> I need to be able to replace the last bit of a bunch of URLs.
> The urls look like this:
> www.somesite.com/some/path/to/something.html
> They may be of varying lengths, but they'll always end with
> .something_or_other.html
> I want to take the "something" and replace it with something else.
> My current plan is to simply do a string.split("/")[-1]
> and then another .split('.') to result in ['something', 'html'], and
> then replace sometihing, and join them together again.
> But - wouldn't it make more sense to do this with re.sub?

Heavens no! Why do you need a 80 pound sledgehammer to crack a peanut???

"Some people, when confronted with a problem, think 'I know, I'll use 
regular expressions.' Now they have two problems." -- Jamie Zawinski

Steven D'Aprano

More information about the Tutor mailing list