%% Replace Repeated Pattern
% Create a character vector with a repeated, overlapping pattern. Compare the results of using the strrep, replace, and regexprep functions to replace the pattern.

repeats = 'abc 2 def 22 ghi 222 jkl 2222'

%
% Find the indices of the repeating pattern '22' using the strfind function. strfind finds all instances of the pattern, including instances that overlap.

indices = strfind(repeats, '22')

%
% Replace '22' using strrep. When you use strrep, it replaces every instance identified by strfind.

using_strrep = strrep(repeats, '22', '*')

%
% Replace '22' using replace. It does not replace every instance that strrep replaces.

using_replace = replace(repeats, '22', '*')

%
% Replace '22' using regexprep. The results are identical to the results using the replace function.

using_regexprep = regexprep(repeats, '22', '*')

% strrep finds all instances of a pattern before replacing any instance. However, the replace and regexprep functions replace an instance of a pattern as soon as they find it within the text.
% Copyright 2012 The MathWorks, Inc.