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

Fixed regular expressions.

parent 4650a475
......@@ -19,6 +19,9 @@ function p = extract(o, varargin) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
useimplicitloops = false;
useregularexpression = false;
p = dseries();
% Get the names of the variables to be extracted from dseries object B.
......@@ -30,21 +33,32 @@ for i=1:nargin-1
if mod(length(idArobase),2)
error('dseries::extract: (Implicit loops) The number of @ symbols must be even!')
end
if ~isempty(idArobase)
useimplicitloops = true;
end
% Regular expression
idBracket.open = strfind(VariableName,'[');
idBracket.close = strfind(VariableName,']');
if ~isequal(length(idBracket.open),length(idBracket.open))
if isequal(idBracket.open(1), 1) && isequal(idBracket.close(end), length(VariableName))
useregularexpression = true;
end
% Check that square brackets are not used, unless extract method is called with a regular expression.
if ~useregularexpression
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))
error('dseries::extract: (Matlab/Octave''s regular expressions) Check opening and closing square brackets!')
end
% Loops and regular expressions are not compatible
if length(idArobase) && length(idBracket.open)
error(['dseries::extract: You cannot use implicit loops and regular expressions in the same rule!'])
if useregularexpression && useimplicitloops
error('dseries::extract: You cannot use implicit loops and regular expressions in the same rule!')
end
% Update the list of variables.
if length(idArobase)
if useimplicitloops
VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_);
elseif length(idBracket.open)
VariableName_ = build_list_of_variables_with_regexp(o.name, idBracket, VariableName, VariableName_);
elseif useregularexpression
VariableName_ = build_list_of_variables_with_regexp(o.name, VariableName(2:end-1));
else
VariableName_ = varargin(:);
end
......
function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, idBrackets, VariableName, list_of_variables)
function list_of_variables = build_list_of_variables_with_regexp(o_list_of_variables, VariableName)
% Copyright (C) 2016-2017 Dynare Team
%
......@@ -17,40 +17,17 @@ function list_of_variables = build_list_of_variables_with_regexp(o_list_of_varia
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
first_block_id = 0;
last_block_id = 0;
matched_strings = regexp(o_list_of_variables, VariableName, 'match');
matched_strings_ = {};
idVariables = find(isnotempty_cell(regexp(o_list_of_variables,VariableName,'match')));
if isempty(idVariables)
error(['dseries::regexp: Can''t find any variable matching ' VariableName ' pattern!'])
end
idVariables_ = [];
for j = 1:length(idVariables)
first_block_flag = 0;
if (first_block_id && strcmp(o_list_of_variables{idVariables(j)}(1:first_block_id),VariableName(1:first_block_id))) || ~first_block_id
first_block_flag = 1;
end
last_block_flag = 0;
if (last_block_id && strcmp(o_list_of_variables{idVariables(j)}(end-last_block_id:end),VariableName(end-last_block_id:end))) || ~last_block_id
last_block_flag = 1;
end
if first_block_flag && last_block_flag
idVariables_ = [idVariables_; idVariables(j)];
for i=1:length(matched_strings)
if ~isempty(matched_strings{i})
matched_strings_ = [matched_strings_, matched_strings{i}(1)];
end
end
VariableName = o_list_of_variables(idVariables_);
list_of_variables = vertcat(list_of_variables, VariableName);
list_of_variables = intersect(o_list_of_variables, matched_strings_);
function b = isnotempty_cell(CellArray)
CellArrayDimension = size(CellArray);
b = NaN(CellArrayDimension);
for i=1:CellArrayDimension(1)
for j = 1:CellArrayDimension(2)
b(i,j) = ~isempty(CellArray{i,j});
end
end
if isempty(list_of_variables)
disp(['dseries::extact: The regular expression ''[' VariableName ']'' did not match any variable name!'])
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