>>> url = '<a href="http://www.somesite.com/some/path/to/something.html">http://www.somesite.com/some/path/to/something.html</a>'<br>>>> var = 'anotherthing'<br>>>> i = url.rfind('/') + 1<br>
>>> j = url.rfind('.') <br>>>> if i < j:<br>... newurl = url[:i] + var + url[j:]<br>... else:<br>... newurl = url[:i] + var<br>>>> newurl<br>'<a href="http://www.somesite.com/some/path/to/anotherthing.html">http://www.somesite.com/some/path/to/anotherthing.html</a>'<br>
<br>Changing the url to '<a href="http://www.somesite.com/some/path/to/something">http://www.somesite.com/some/path/to/something</a>' we get newurl as '<a href="http://www.somesite.com/some/path/to/anotherthing">http://www.somesite.com/some/path/to/anotherthing</a>'<br>
<br>However if you are absolutely sure the pattern is '<a href="http://www.somesite.com/some/path/to/something.html">http://www.somesite.com/some/path/to/something.html</a>' , then you can simply write one-liner as:<br>
>>> url[:url.rfind('/') + 1] + var + url[url.rfind('.'):]<br>'<a href="http://www.somesite.com/some/path/to/anotherthing.html">http://www.somesite.com/some/path/to/anotherthing.html</a>'<br>
<br>Literally the same stuff. I don't think you need regex for such simple string manipulation.<br><br>~l0nwlf<br><br><br><div class="gmail_quote">On Sun, Feb 21, 2010 at 4:04 AM, Lao Mao <span dir="ltr"><<a href="mailto:laomao1975@googlemail.com">laomao1975@googlemail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Hello,<div><br></div><div>I need to be able to replace the last bit of a bunch of URLs.</div>
<div><br></div><div>The urls look like this:</div><div><br></div><div><a href="http://www.somesite.com/some/path/to/something.html" target="_blank">www.somesite.com/some/path/to/something.html</a></div>
<div><br></div><div>They may be of varying lengths, but they'll always end with .something_or_other.html</div><div><br></div><div>I want to take the "something" and replace it with something else.</div><div>
<br></div><div>My current plan is to simply do a string.split("/")[-1]</div><div><br></div><div>and then another .split('.') to result in ['something', 'html'], and then replace sometihing, and join them together again.</div>
<div><br></div><div>But - wouldn't it make more sense to do this with re.sub?</div><div><br></div><div>In which case, how would I specify only the bit between the last / and the .html?</div><div><br></div><div>Thanks,</div>
<div><br></div><div>Laomao</div>
<br>_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br>