I am reading whole Python files as single strings, compiling the expressions in multiline-mode. So far I have been able to match single variable assignment with Python regex:
"^\s*[A-Za-z_][A-Za-z_0-9]*\s*(?=\=)(?!==)"
^\s*
: First it checks if variable assignment is on a new line followed by spaces. I do this to prevent syntax such asfoo(required=True, thud=3)
from matching, as I do not define those as variable assignments.[A-Za-z_][A-Za-z_0-9]*
: Then it looks for a valid variable name...\s*(?=\=)(?!==)
: ...and sees if variable name is followed by=
and not by==
, as it is a comparison and not a variable assignment.
This works fine, but not for assignment of multiple variables in a single line:
a, b = 4, 5
In this case the regex will not match either of the variables. Note that it should not match the ,
, only a
and b
separately. How to do this?