%% Replace Substrings That Match Pattern
% Since R2020b
% Create a string that includes a phone number.

str = "Hide the numbers in: (508) 555-1234"

%
% Create a pattern that matches a digit using the digitsPattern function.

pat = digitsPattern(1)

%
% Replace all digits with a "#" character.

newStr = replace(str,pat,"#")

%
% Create another pattern that matches only phone numbers.

pat = "(" + digitsPattern(3) + ") " + digitsPattern(3)  + "-" + digitsPattern(4)

%
% Replace a phone number in a string that also has another number.

str = "12 calls made to: (508) 555-1234";
newStr = replace(str,pat,"(###) ###-####")

%
% For a list of functions that create pattern objects, see pattern.
% Copyright 2020 The MathWorks, Inc.