Commit cf4e5cb7 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Added the possibility to select variables with wildcard parameters (*).

parent a79d990d
......@@ -21,6 +21,7 @@ function p = extract(o, varargin) % --*-- Unitary tests --*--
useimplicitloops = false;
useregularexpression = false;
usewildcardparameter = false;
p = dseries();
......@@ -36,18 +37,33 @@ for i=1:nargin-1
if ~isempty(idArobase)
useimplicitloops = true;
end
% Regular expression
% Regular expression.
idBracket.open = strfind(VariableName,'[');
idBracket.close = strfind(VariableName,']');
if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName))
useregularexpression = true;
if ~isempty(idBracket.open) && ~isempty(idBracket.close)
if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName))
useregularexpression = true;
end
end
% Check that square brackets are not used, unless extract method is called with a regular expression.
% Wildcard parameter.
if ~useregularexpression
idStar = strfind(VariableName,'*');
if ~isempty(idStar)
usewildcardparameter = true;
useregularexpression = true;
VariableName = sprintf('[%s]', VariableName);
VariableName = strrep(VariableName, '*', '\w*');
end
end
% Check that square brackets are not used, unless extract method is called with a regular expression.
if ~useregularexpression && ~(isempty(idBracket.open) && isempty(idBracket.close))
error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!')
end
if useregularexpression && usewildcardparameter && ~(isempty(idBracket.open) && isempty(idBracket.close))
error('dseries::extract: Square brackets are not allowed, unless regexp are used to select variables!')
end
% Check that square brackets in regular expressions
if useregularexpression && ~isequal(length(idBracket.open),length(idBracket.open))
if useregularexpression && ~usewildcardparameter && ~isequal(length(idBracket.open),length(idBracket.open))
error('dseries::extract: (Matlab/Octave''s regular expressions) Check opening and closing square brackets!')
end
% Loops and regular expressions are not compatible
......@@ -58,7 +74,7 @@ for i=1:nargin-1
if useimplicitloops
VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_);
elseif useregularexpression
VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1));
VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1), usewildcardparameter);
else
VariableName_ = varargin(:);
end
......
function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, VariableName)
function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, VariableName, wildcardparameterflag)
% Copyright (C) 2016-2017 Dynare Team
%
......@@ -29,5 +29,10 @@ end
list_of_variables = intersect(o_list_of_variables, matched_strings_);
if isempty(list_of_variables)
disp(['dseries::extact: The regular expression ''[' VariableName ']'' did not match any variable name!'])
if wildcardparameterflag
VariableName = strrep(VariableName, '\w*', '*');
disp(['dseries::extact: The wildcard expression ''' VariableName ''' did not match any variable name!'])
else
disp(['dseries::extact: The regular expression ''[' VariableName ']'' did not match any variable name!'])
end
end
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment