%% Correct Erroneous Spaces
% Use whitespacePattern to correct spacing when more than one whitespace character exists.
% Create txt as a string. Create pat as a pattern object that matches 2 or more whitespace characters using whitespacePattern. Replace the parts of text matched with a single space.

txt = "Text looks   strange    with    extra    spaces";
pat = whitespacePattern(2,inf);
txt = replace(txt,pat," ")

% Copyright 2020 The MathWorks, Inc.