I am trying to find the class name where a function name is called.
E.g. suppose I have:
class foo1(object):
bar1()
class foo2(object):
bar2()
class foo3(object):
bar3()
If I am searching for bar2() I want to receive:
class foo2(object):
bar2()
I tried regex class[\s\S]*bar2\(\)(?!class)
with the idea being that I would negative lookahead for another appearance of class. Unfortunately it looks like the [\s\S] is already matching everything: https://regex101.com/r/kZ7eE5/1
Is there a way to match if "class" appears only one time while matching on all other chars (including new line and tab)?
Also alternatives that do not require regex would be accepted.
The RE approach can be error prone (the expressive power of Python language is more that those of the regular languages recognized by REs).
Use Python's
ast
module to parse Python code:and you can do many other things, check the docs at https://greentreesnakes.readthedocs.org/en/latest/
Edit: a more complete example: