pythonpython-3.xpython-re

get all calling function inside a function


I'm trying to extract string which comes before "(" followed by ")". Note : there may or maybe nothing in between the braces.

string = "int a = {0}; func1(arg1, arg11); if(var1 == data1 && func2(arg2)) {  var2 = func3(arg3, func4(arg4))  func5(var5) } else { (void)func6(arg6) }"

Tried (in Python 3.12.3):

  1. using re module

     re.findall(r'.*?(?=\s?\().*?(?=\))', string)
     re.findall(r'.*?(?=\s?\()', string)
    
  2. using regex

      regex.search(r'.*?(\((.*?:[()]|[^()]|(?R))*+\))', string)
    

desired output:

func1, if, func2, func3, func4, func5, func6

Is there any way to recursively call regex?


Solution

  • re.findall(r'\w+?(?=\s?\(.*?\))', string)
    

    You need to use \w+? at the beginning so the match is just a single token, not everything before the (.