function matches = regexps(string, expression, varargin)
%REGEXPS Match regular expression.
% MATCHES = REGEXPS(STRING,EXPRESSION) returns a structure array of the search
% results with the elements: MATCHES.match which is the string that represents
% the match; and MATCHES.tokens which is a cell array of strings that reprent
% the tokens for that match.
%
% By default, REGEXPS returns all matches and is case sensitive
% Available options are:
%
% 'ignorecase' - Ignore the case of characters when matching EXPRESSION
% to STRING.
% 'once' - Return only the first occurrence of EXPRESSION in
% STRING.
%
% Example
% str = 'six sides of a hexagon';
% pat = 's(\w*)s';
% matches = regexps(str, pat)
% returns
% matches.match = 'sides'
% matches.tokens = {'ide'}
%
% See also REGEXP, REGEXPREP
% J Breslau
% Copyright (c) 1995-2002 The MathWorks, Inc.
try
[first last tokens] = regexp(string, expression, varargin{:});
catch
% use regexprep to make the error look like it came from here.
errStr = regexprep(lasterr, '^.*?\n([^\n]*)$', '$1', 'tokenize');
errStr = regexprep(errStr, 'regexp\w*', 'regexps', 'preservecase', 'once');
error(errStr);
end
if (iscell(first))
if (~iscell(string))
string = {string};
end
dims = size(first);
for row = 1:dims(1)
for col = 1:dims(2)
matches{row, col} = parseResults(first{row, col}, last{row, col}, tokens{row, col}, string{row});
end
end
else
matches = parseResults(first, last, tokens, string);
end
function matches = parseResults(first, last, tokens, string)
matches = [];
for i = 1:length(first)
matches(i).match = string(first(i):last(i));
matches(i).tokens = {};
for j = 1:size(tokens{i},1)
if tokens{i}(j,1)
matches(i).tokens{j} = string(tokens{i}(j,1):tokens{i}(j,2));
end
end
end