📄 mstrrep.m
字号:
function [text, wasp] = mstrrep(text, pats, reps, varargin)
% mstrrep - replace multiple patterns (multi strrep)
%
% FORMAT: text = mstrrep(text, patterns, replaces [, rx])
%
% Input fields:
%
% text string to be patched
% patterns 1xN cell array with patterns
% replaces 1xN (or 1x1) cell array with replacement strings
% rx if given and true, use regexprep
%
% Output fields:
%
% text patched string
% waspatched string altered flag (convenience only, calls strcmp)
%
% Note: regexprep is called with 'ignorecase' and 'tokenize' options.
%
% See also strrep, regexprep, strcmp
% Version: v0.5c
% Build: 6120415
% Date: Dec-04 2006, 3:15 PM CET
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% argument check
if nargin < 3 || ...
~ischar(text) || ...
~iscell(pats) || ...
isempty(pats) || ...
~iscell(reps) || ...
isempty(reps) || ...
all(numel(reps) ~= [1, numel(pats)])
error( ...
'BVQXtools:BadArgument', ...
'Invalid combination of arguments.' ...
);
end
text = text(:)';
pats = pats(:)';
reps = reps(:)';
npat = length(pats);
if length(reps) == 1
reps(2:npat) = reps;
end
% keep original state for comparison only if requested!
if nargout > 1
texo = text;
end
% strrep or regexp
userx = false;
if nargin > 3 && ...
numel(varargin{1}) == 1 && ...
(isnumeric(varargin{1}) || ...
islogical(varargin{1}))
if varargin{1}
userx = true;
if mainver > 6
rxargs = {'ignorecase'};
else
rxargs = {'ignorecase', 'tokenize'};
end
end
end
% what to do
try
if userx
for pc = 1:npat
text = regexprep(text, pats{pc}, reps{pc}, rxargs{:});
end
else
for pc = 1:npat
text = strrep(text, pats{pc}, reps{pc});
end
end
catch
error( ...
'BVQXtools:BadArgument', ...
'Invalid pattern/replacement combination for pattern %d.', ...
pc ...
);
end
% second argout
if nargout > 1
wasp = logical(strcmp(texo, text));
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -