&gt;&gt;&gt; url = &#39;<a href="http://www.somesite.com/some/path/to/something.html">http://www.somesite.com/some/path/to/something.html</a>&#39;<br>&gt;&gt;&gt; var = &#39;anotherthing&#39;<br>&gt;&gt;&gt; i = url.rfind(&#39;/&#39;) + 1<br>

&gt;&gt;&gt; j = url.rfind(&#39;.&#39;) <br>&gt;&gt;&gt; if i &lt; j:<br>...     newurl = url[:i] + var + url[j:]<br>... else:<br>...     newurl = url[:i] + var<br>&gt;&gt;&gt; newurl<br>&#39;<a href="http://www.somesite.com/some/path/to/anotherthing.html">http://www.somesite.com/some/path/to/anotherthing.html</a>&#39;<br>

<br>Changing the url to &#39;<a href="http://www.somesite.com/some/path/to/something">http://www.somesite.com/some/path/to/something</a>&#39; we get newurl as &#39;<a href="http://www.somesite.com/some/path/to/anotherthing">http://www.somesite.com/some/path/to/anotherthing</a>&#39;<br>

<br>However if you are absolutely sure the pattern is &#39;<a href="http://www.somesite.com/some/path/to/something.html">http://www.somesite.com/some/path/to/something.html</a>&#39; , then you can simply write one-liner as:<br>

&gt;&gt;&gt; url[:url.rfind(&#39;/&#39;) + 1] + var + url[url.rfind(&#39;.&#39;):]<br>&#39;<a href="http://www.somesite.com/some/path/to/anotherthing.html">http://www.somesite.com/some/path/to/anotherthing.html</a>&#39;<br>

<br>Literally the same stuff. I don&#39;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">&lt;<a href="mailto:laomao1975@googlemail.com">laomao1975@googlemail.com</a>&gt;</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&#39;ll always end with .something_or_other.html</div><div><br></div><div>I want to take the &quot;something&quot; and replace it with something else.</div><div>


<br></div><div>My current plan is to simply do a string.split(&quot;/&quot;)[-1]</div><div><br></div><div>and then another .split(&#39;.&#39;) to result in [&#39;something&#39;, &#39;html&#39;], and then replace sometihing, and join them together again.</div>


<div><br></div><div>But - wouldn&#39;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>