> > They may be of varying lengths, but they'll always end with > .something_or_other.html >
Missed this point. You can use posixpath module too as an option. >>> url = 'http://www.somesite.com/some/path/to/something.html >>> var = 'anotherthing' >>> posixpath.dirname(url) 'http://www.somesite.com/some/path/to' >>> posixpath.dirname(url) + '/' + var + '.html' 'http://www.somesite.com/some/path/to/anotherthing.html' On Sun, Feb 21, 2010 at 4:45 AM, Shashwat Anand <anand.shash...@gmail.com>wrote: > >>> url = 'http://www.somesite.com/some/path/to/something.html' > >>> var = 'anotherthing' > >>> i = url.rfind('/') + 1 > >>> j = url.rfind('.') > >>> if i < j: > ... newurl = url[:i] + var + url[j:] > ... else: > ... newurl = url[:i] + var > >>> newurl > 'http://www.somesite.com/some/path/to/anotherthing.html' > > Changing the url to 'http://www.somesite.com/some/path/to/something' we > get newurl as 'http://www.somesite.com/some/path/to/anotherthing' > > However if you are absolutely sure the pattern is ' > http://www.somesite.com/some/path/to/something.html' , then you can simply > write one-liner as: > >>> url[:url.rfind('/') + 1] + var + url[url.rfind('.'):] > 'http://www.somesite.com/some/path/to/anotherthing.html' > > Literally the same stuff. I don't think you need regex for such simple > string manipulation. > > ~l0nwlf > > > On Sun, Feb 21, 2010 at 4:04 AM, Lao Mao <laomao1...@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? >> >> Thanks, >> >> Laomao >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor