MATLAB: How to replace an entire word with another

regexprepreplacetext analysisword processing

I'm trying to replace a word from a string with another. For example I have to replace b4 with before and b4n with before now, but if I use the commands replace or regexprep the output for b4n is beforen. How can I match the entire word and not the characters?

Best Answer

Try this:
str = 'b4 b4n';
out = regexprep(str, '\<b4\>', 'before');
out = regexprep(out, '\<b4n\>', 'before now')
out =
before before now