Or if you want to do this on any file extension and make sure that nothing in the line is changed except the last.
import re str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.jpg" re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.png" re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.gif" re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.zip" re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str)
Exit:
>>> str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.jpg" >>> re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) 'http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_m.jpg' >>> str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.png" >>> re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) 'http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_m.png' >>> str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.gif" >>> re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) 'http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_m.gif' >>> str = "http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_s.zip" >>> re.sub("(.*)_s(\.[a-z0-9]{1,4})$", r"\1_m\2", str) 'http://farm6.static.flickr.com/5149/5684108566_aed8b9b52d_m.zip'
source share