4

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?

goaran
  • 353
  • 2
  • 11

1 Answers1

6

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.

  • exprtk::collect_variables does not work with following expressions LANGUAGE in ('SPANISH' + 'ENGLISH' + 'URDU') and PROFICIENCY > 0 101_Courses > 100 Is there a limitation to it? – Mirza Talha Jun 01 '20 at 14:41
  • @MirzaTalha the expression is malformed, there is a missing operator in: "0 101_Courses" the error context produced post compile provides all the details you need to detect and resolve the issue. – ExprTk Developer Oct 17 '20 at 18:57