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

1 次查看(过去 30 天)
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

采纳的回答

MathWorks Support Team
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

产品


版本

R2009a

Community Treasure Hunt

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

Start Hunting!

Translated by