0

I've got a String that represents a function call that I'm trying to parse. Each argument can either be another function call, or a single word. I would like to write an algorithm that will retrieve the name, and split the args.

class FunctionCall{
   String name;
   String[] args;
}

function parseFunction(String func){
    FunctionCall fc = new FunctionCall();
    fc.name = func.subString(0,func.indexOf("("))
    fc.args = ???
}

A function call could look like func1(func2(arg1, arg2),func3(func4(arg3)),arg4), and I would like String[] args to contain func2(arg1, arg2), func3(func4(arg3)), and arg4.

I don't believe this is possible with regex due to the recursive nature of function calls.

I writing the code in Java, but other languages would be fine.

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56

2 Answers2

0

It seems that I have found the answer described here

I need to parse the characters one by one, and count the number of opening parenthesis. If I come across a comma when no parenthesis is open, then I should split on that point.

Community
  • 1
  • 1
Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
0
(?:^[^\(\)]*?\()?(?:(.*?\)),|(.*?)\)$)

You can try this.It is matching the current input.Might require more input patterns to test.

See demo.

http://regex101.com/r/pP3pN1/31

vks
  • 67,027
  • 10
  • 91
  • 124