


STRMATCHRE Select strings that match a regular expression.
MATCHSTRS = STRMATCHRE(PATTERN, STRS) returns the rows of the character
array or elements of the cell array of strings STRS that match the
regular expression PATTERN (see REGEXP for the syntax of PATTERN).
Matched strings must match the entire PATTERN -- so 'testing' matches
'.*ing' but not 'ing'.
If PATTERN is a cell array of regular expressions, MATCHSTRS contains
the strings from STRS that match at least one of the patterns.
[MATCHSTRS, INDS] = STRMATCHRE(PATTERN, STRS) also returns the indices
into STRS such that MATCHSTRS equals STRS(INDS,:) if STRS is a
character array or STRS{INDS} if STRS is a cell array.
Example:
D = dir; filenames = {D.name};
STRMATCHRE(filenames, '.*\.m$')
returns the names of files in the current directory with
extension .m
See also STRMATCH, REGEXP.

0001 function [matchstrs,inds] = strmatchre(pattern, strs) 0002 %STRMATCHRE Select strings that match a regular expression. 0003 % MATCHSTRS = STRMATCHRE(PATTERN, STRS) returns the rows of the character 0004 % array or elements of the cell array of strings STRS that match the 0005 % regular expression PATTERN (see REGEXP for the syntax of PATTERN). 0006 % Matched strings must match the entire PATTERN -- so 'testing' matches 0007 % '.*ing' but not 'ing'. 0008 % 0009 % If PATTERN is a cell array of regular expressions, MATCHSTRS contains 0010 % the strings from STRS that match at least one of the patterns. 0011 % 0012 % [MATCHSTRS, INDS] = STRMATCHRE(PATTERN, STRS) also returns the indices 0013 % into STRS such that MATCHSTRS equals STRS(INDS,:) if STRS is a 0014 % character array or STRS{INDS} if STRS is a cell array. 0015 % 0016 % Example: 0017 % D = dir; filenames = {D.name}; 0018 % STRMATCHRE(filenames, '.*\.m$') 0019 % returns the names of files in the current directory with 0020 % extension .m 0021 % 0022 % See also STRMATCH, REGEXP. 0023 0024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Parse Inputs %%%%%%%%%%%%%%%%%%%%%%%%%%% 0025 % Coerce strs to a 1 column cell array 0026 origcell = iscell(strs); 0027 if (~origcell), strs = cellstr(strs); end; 0028 strs = strs(:); 0029 0030 % We'll need a list of string lengths later to make sure we matched all 0031 % the way to the end 0032 if (iscell(pattern)), numpat = length(pattern); 0033 else numpat = 1; 0034 end 0035 strlens = repmat(cellfun('length', strs), 1, numpat); 0036 0037 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Match Pattern %%%%%%%%%%%%%%%%%%%%%%%%%%% 0038 [start,finis] = regexp(strs, pattern); 0039 0040 %%%%%%%%%%%%%%%%%% Convert REGEXP output to indices %%%%%%%%%%%%%%%%%% 0041 % Change failed matches from [] to 0 so we can convert to a matrix. 0042 failed = [cellfun('length', start) ~= 1]; % (>1 match means failed too) 0043 [start{failed}] = deal(0); [finis{failed}] = deal(0); 0044 start = cell2mat(start); finis = cell2mat(finis); 0045 0046 % Only take strings that matched at least one pattern beginning to end 0047 start(start ~= 1) = 0; finis(finis ~= strlens) = 0; 0048 inds = find(any(start,2) & any(finis,2)); 0049 0050 %%%%%%%%%%%%%%%%%%%%%%%%% Construct outputs %%%%%%%%%%%%%%%%%%%%%%%%%% 0051 matchstrs = strs(inds); 0052 if (~origcell), matchstrs = char(matchstrs); end; 0053 if (nargout == 1), clear inds; end;