www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ReturnBothMatchingandNonmatchingSubstringsExample.m

    %% Return Both Matching and Nonmatching Substrings
% Capture parts of a character vector that match a regular expression using the |'match'|
% keyword, and the remaining parts that do not match using the |'split'|
% keyword.   

%%  
str = 'She sells sea shells by the seashore.';
expression = '[Ss]h.';
[match,noMatch] = regexp(str,expression,'match','split') 

%%
% The regular expression |'[Ss]h.'| specifies that: 
%  

%%
% * |S| or |s| is the first character.  

%%
% * |h| is the second character.  

%%
% * The third character can be anything, including a space, as indicated
% by the dot (|.|).   

%%
% When the first (or last) character in a character vector matches a regular expression,
% the first (or last) return value from the |'split'| keyword is an empty
% character vector.  

%% 
% Optionally, reassemble the original character vector from the substrings. 
combinedStr = strjoin(noMatch,match)