ExprTk, get a list of symbols/variables in expression

750 views Asked by At

I want to get a list of the symbols out of an expression in ExprTk (not the ones I registered, but the ones that are in the expression. E.g. when the expression is

const std::string expression_string = "abs(sin(x)^2+5*y)";

I need to get x and y as result as a list/vector or something. How can I do this?

1

There are 1 answers

4
ExprTk Developer On BEST ANSWER

In the ExprTk readme.txt: Section 23 - Helpers & Utils has the following helper free function: collect_variables

Usage is as follows:

  const std::string expression_string = "abs(sin(x)^2+5*y)";

  std::vector<std::string> variable_list;

  if (exprtk::collect_variables(expression_string, variable_list))
  {
     for (const auto& var : variable_list)
     {
        ...
     }
  }
  else
    printf("An error occurred.");

Note: If the expression is invalid for any reason collect_variables will return false.