MATLAB: Changing letters to other letters with regexprep

regexprepscriptstring

I'm trying to change letters in a string to different ones with one line of code. I simplified my code down to the following:
regexprep('ab',{'a','b'},{'b','c'});
Ideally, this would output 'bc'. Currently, regexprep runs through the string twice and ends up outputting 'cc'.
Is there any way to prevent this from happening? Is it possible with regexprep? Also, I would prefer to keep the letters as strings, as I'm planning on using the 'preservecase' condition.

Best Answer

You don't need regexprep, using ismember and indexing is simpler:
>> str = 'a':'z'
str = abcdefghijklmnopqrstuvwxyz
>> old = 'bcd';
>> new = 'abc';
>> [X,Y] = ismember(str,old);
>> str(X) = new(Y(X))
str = aabcefghijklmnopqrstuvwxyz
Note that this works efficiently for any characters, not just a limited subset. In order to use regexprep you would probably need to use a dynamic replacement expression.