I have a python file as
test.py
import os
class test():
def __init__(self):
pass
def add(num1, num2):
return num1+num2
I am reading this file in a string as :
with open('test.py', 'r') as myfile:
data=myfile.read()
print data
Now, my data contains the string with all lines and new lines. I need to find lines with start of class and def.
for example:
I need the output to be printed as :
class test():
def __init__(self):
def add(num1, num2):
How can I process this using regular expressions?
process?re.findall(r'(?m)^[ \t]*((?:class|def) .*)', data). Regex demo.