Как я могу захватить класс и методы из файла python?Regex для захвата класса и методов
Меня не интересуют attrs или args.
class MyClass_1(...):
...
def method1_of_first_class(self):
...
def method2_of_first_class(self):
...
def method3_of_first_class(self):
...
class MyClass_2(...):
...
def method1_of_second_class(self):
...
def method2_of_second_class(self):
...
def method3_of_second_class(self):
...
То, что я пытался до сих пор:
class ([\w_]+?)\(.*?\):.*?(?:def ([\w_]+?)\(self.*?\):.*?)+?
Варианты: точка соответствует новой строки
захватывая КЛАССА
Match the characters “class ” literally «class »
Match the regular expression below and capture its match into backreference number 1 «([\w_]+?)»
Match a single character present in the list below «[\w_]+?»
Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
A word character (letters, digits, etc.) «\w»
The character “_” «_»
Match the character “(” literally «\(»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “)” literally «\)»
Match the character “:” literally «:»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
захватывая МЕТОДЫ:
Match the regular expression below «(?:def ([\w_]+?)\(self.*?\):.*?)+?»
Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
Match the characters “def ” literally «def »
Match the regular expression below and capture its match into backreference number 2 «([\w_]+?)»
Match a single character present in the list below «[\w_]+?»
Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
A word character (letters, digits, etc.) «\w»
The character “_” «_»
Match the character “(” literally «\(»
Match the characters “self” literally «self»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “)” literally «\)»
Match the character “:” literally «:»
Match any single character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Но это только захватывает имя класса и первый метод, я думаю, что это потому, что обратная ссылка номер 2 не может захватить больше чем 1, даже то, что находится внутри (? Myregex) +?
Выходной ток:
'MyClass_1':'method1_of_first_class',
'MyClass_2':'method1_of_second_class'
Желаемая Выход:
'MyClass_1':['method1_of_first_class','method2_of_first_class',...],
'MyClass_2':['method1_of_second_class','method2_of_second_class',...]
Каков ваш ожидаемый результат? –
'[MyClass_1, [method1_of_first_class, method2_of_first_class, ...]]' '[MyClass_2, [method1_of_second_class, method2_of_second_class, ...]]' –
Анализ кода с регулярным выражением ** жесткий **. См. [1] (http://stackoverflow.com/a/27149898/), [2] (http://stackoverflow.com/a/17134110), [3] (http://stackoverflow.com/a/ 21395083). Я бы предложил использовать выделенный парсер. Также, задавая вопросы регулярного выражения, укажите язык/инструмент, который вы используете. – HamZa