Search and replace with “whole word only” option [duplicate]

You want a regular expression. You can use the token \b to match a word boundary: i.e., \bresult\b would match only the exact word “result.”

import re

with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor = re.sub(r"\b%s\b" % s[0] , s[1], editor)

Leave a Comment