Home > chronux_2_00 > spikesort > utility > matlabtools > strmatchre.m

strmatchre

PURPOSE ^

STRMATCHRE Select strings that match a regular expression.

SYNOPSIS ^

function [matchstrs,inds] = strmatchre(pattern, strs)

DESCRIPTION ^

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.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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;   pattern = {pattern};
0034 end
0035 strlens = repmat(cellfun('length', strs), 1, numpat);
0036 
0037 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Match Pattern %%%%%%%%%%%%%%%%%%%%%%%%%%%
0038 start = {};  finis = {};
0039 for p = 1:numpat
0040     [sta,fin] = regexp(strs, pattern{p});
0041     start = cat(2,start,sta);  finis = cat(2,finis,fin);
0042 end
0043 
0044 %%%%%%%%%%%%%%%%%% Convert REGEXP output to indices %%%%%%%%%%%%%%%%%%
0045 % Change failed matches from [] to 0 so we can convert to a matrix.
0046 failed = (cellfun('length', start) ~= 1);  % (>1 match means failed too)
0047 [start{failed}] = deal(0);   [finis{failed}] = deal(0);
0048 start = cell2mat(start);     finis = cell2mat(finis);
0049 
0050 % Only take strings that matched at least one pattern beginning to end
0051 start(start ~= 1) = 0;       finis(finis ~= strlens) = 0;
0052 inds = find(any(start,2) & any(finis,2));
0053 
0054 %%%%%%%%%%%%%%%%%%%%%%%%% Construct outputs %%%%%%%%%%%%%%%%%%%%%%%%%%
0055 matchstrs = strs(inds);
0056 if (~origcell), matchstrs = char(matchstrs); end;    
0057 if (nargout == 1), clear inds; end;

Generated on Fri 15-Aug-2008 11:35:42 by m2html © 2003