Suppose I have a line such as:
"func(arg1, arg2, arg3, arg4, ..., argn)"
EDIT: This function is not in a specific language. It just has this format. If this simplifies, do not think of it as a function call, just a string.
I want to write a regular expression to match a function and each argument. I am writing this in Python. Desired Result:
{"function" : "func", "arg" : ["arg1", "arg2", ... , "argn"]}
EDIT: Although arguments can be functional calls, I can easily recursively try to match them with the same regex when I create one that works. By this, I mean that I can return a function with each of the arguments. But this is not very relevant. I am not , trying to create an interpreter, just something to recognize the arguments.
Here is my attempt:
import re s = "func(arg1, arg2, arg3, arg4, argn)" m = re.match(r"(?P<function>\w+)\s?\((?P<args>(?P<arg>\w+(,\s?)?)+)\)", s) print m.groupdict()
And here is the conclusion:
{'function': 'func', 'args': 'arg1, arg2, arg3, arg4, argn', 'arg': 'argn'}
A function matches only a penalty, as does a set of arguments. However, it seems that I cannot match individual arguments. Is this a problem with my regex, or a Python regex matching restriction?
EDIT2: I know that now I can separate the arguments using the following code:
d["arg"] = d["args"].split(", ")
But I was wondering if I can do all the work with regular expressions. In particular, I wonder why "arg" matches only the last argument.
EDIT3: I suppose I was (1) hoping to find out why Python only matches the last argument each time, and (2) can I do Python-style pattern matching in Python. Or if Python has something as intuitive as pattern matching in Scheme style. I looked at the ast module, and its syntax is prohibitively complex.