Why do I receive unexpected results when performing string find and replace using STRREP in MATLAB 7.6 (R2008a)?

1 view (last 30 days)
When I try to perform a string replace as in the example below (with strings of repeating characters), MATLAB provides an unexpected result. It replaces each character in the string rather than the entire string.
x = 'abcd 2 efgh 22 ijkl 222 mnop 2222 qrst 22222 uvw 222222 xyz'
y = strfind(x, '22')
z = strrep(x, '22', '*')
results in:
x =
abcd 2 efgh 22 ijkl 222 mnop 2222 qrst 22222 uvw 222222 xyz
y =
13 21 22 30 31 32 40 41 42 43 50 51 52 53 54
z =
abcd 2 efgh * ijkl ** mnop *** qrst **** uvw ***** xyz

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 May 2010
This change has been incorporated into the documentation in Release 2010a (R2010a). For previous releases, read below for any additional information:
This due to a full find being performed and then a replace. If the desired behavior is a find alternated with a replace (so that overlapping matches are not found) then REGEXPREP may be used.
An example that works as desired for the code above is:
regexprep(x, '22', '*')
ans =
abcd 2 efgh * ijkl *2 mnop ** qrst **2 uvw *** xyz

More Answers (0)

Products


Release

R2009a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!