I really dont know how to word this. I am creating a program that reads through another py file called code.py, it will find all VALID dictionary variable names and print them, easy enough? But the code im trying to run through is extremely tricky, purposely put in examples to trick the regex. The test code for code.py is here and my current code is:
import re
with open ("code.py", "r") as myfile:
data=myfile.read()
potato = re.findall(r' *(\w+)\W*{',data,re.M)
for i in range(len(potato)):
print(potato[i])
That regex doesnt work 100%, when used on the test code it will print variables that arent meant to be printed such as:
# z={}
z="z={}"
print('your mother = {}')
The expected output for the test file is a0, a, b ,c d, e, etc all the way down to z, then it will be aa, ab , ac, ad, etc all the way down to aq
and anything really labeled z in the test code shouldnt print. I realise that regex isn't amazing for doing this but i have to use regex and it can be done.
EDIT: Using the new regex (r'^ (\w+)\W{',data,re.M) the output fails on examples where there are variables assigned on one line such as,
d={
};e={
};