Matching at the End of a String : sub « Regular Expressions « Python Tutorial






import re


s = '100 NORTH MAIN ROAD' 
s.replace('ROAD', 'RD.')                  
s = '100 NORTH BROAD ROAD' 
s.replace('ROAD', 'RD.')                       
s[:-4] + s[-4:].replace('ROAD', 'RD.')        
import re                                       

re.sub('ROAD$', 'RD.', s)








16.7.sub
16.7.1.re.sub substitutes the leftmost, nonoverlapping occurrences of a pattern with a given replacement
16.7.2.Matching at the End of a String
16.7.3.Matching Whole Words
16.7.4.Searching and Replacing with sub() [and subn()]