MATLAB: How to replace a certain word in a plain text

replacetxt

Hi all, I have a text and I want to replace the word 'the' with 'a' ,I used regexpi but the problem is a word that contains the letters of "the" through it's letters also replaced….for example 'rather' becomes 'raar' ,can you please help me to solve this problem.

Best Answer

s = 'This is the string. The goal is to replace the thes with as';
newstr = regexprep(s, '\<the\>', 'a', 'preservecase');
This matches only words, replacing 'the' with 'a', and when 'The' is matched it replaces with 'A' (conserving case)