regex - Regular expression MATLAB -
i have tried solve problem reading old questions , googles help.
i writing short script in matlab user types in equation , plot data using eval.
but want check if equation right , uses right variables , so...
i have 3 variables, x,y,z upper case, example 'x+y-z-7.5' solid equation, 'xb-z' isn't. 'x' solid "equation"...
how can write expression? here have...
regexp(test,'(x|y|z)$|(x|y|z|\d)&&(+|-|*|/|)') my next plan like,
if regexp(test,'(x|y|z)$|(x|y|z|\d)&&(+|-|*|/|)') == 1 disp ('correct') end so want regexp return if string matches whole expression, not startindex. have problem fix too.
please, i'm stuck.
one potential solution (if have symbolic math toolbox) rely on determine whether equations valid.
you can use symvar extract symbols used in equation , compare these variables allow.
allowed = {'x', 'y', 'z'}; vars = symvar(userinput); tf = ismember(vars, allowed); if ~all(tf) disp('invalid variables used'); end this going more robust attempting create regular expressions relies on matlab's internal parser.
Comments
Post a Comment