[Tutor] Replacing part of a URL

Kent Johnson kent37 at tds.net
Sun Feb 21 00:44:04 CET 2010


On Sat, Feb 20, 2010 at 5:34 PM, Lao Mao <laomao1975 at googlemail.com> 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?
> In which case, how would I specify only the bit between the last / and the
> .html?

In [11]: import re

In [13]: re.sub(r'/[^/]+\.html', '/something_else.html',
'www.somesite.com/some/path/to/something.html')
Out[13]: 'www.somesite.com/some/path/to/something_else.html'

Kent


More information about the Tutor mailing list