adplus-dvertising

How to extract only variables from python code expression without any function name

Asked 3 months ago
Viewed 11 times

I've a formula expression stored in a string and I want to extract variables only out of that string expression. This expression is basically a python code which calls some function in it.

For example,

expr = "sum(x)"

I want to extract only variable x from this string. It should discard sum from the string.

I tried py_expression_eval and AST which gives both sum and x as variables whereas I want only x from the parser.

asked 3 months ago

Correct Answer

If I got your question correctly then

expr[expr.find("(") + 1:-1]

will do the trick.

answered 3 months ago