The objective of this Regex (\w*)\s*\([(\w*),]*\)
is to get a function name and its arguments.
For example, given f1 (11,22,33)
the Regex should capture four elements:
f1
11
22
33
What's wrong with this regex?
The objective of this Regex (\w*)\s*\([(\w*),]*\)
is to get a function name and its arguments.
For example, given f1 (11,22,33)
the Regex should capture four elements:
f1
11
22
33
What's wrong with this regex?
You can do it with split
Here is an example in javascript
var ar = str.match(/\((.*?)\)/);
if (ar) {
var result = ar[0].split(",");
}
Reference: https://stackoverflow.com/a/13953005/1827594
Some things are hard for regexes :-)
As the commenters above are saying, '*' can be too lax. It means zero or more. So foo(,,)
also matches. Not so good.
(\w+)\s*\((\w+)(?:,\s*(\w+)\s*)*\)
That is closer to what you want I think. Let's break that down.
\w+ <-- The function name, has to have at least one character
\s* <-- zero or more whitespace
\( <-- parens to start the function call
(\w+) <-- at least one parameter
(?:) <-- this means not to save the matches
,\s* <-- a comma with optional space
(\w+) <-- another parameter
\s* <-- followed by optional space
This is the result from Python:
>>> m = re.match(r'(\w+)\s*\((\w+)(?:,\s*(\w+)\s*)*\)', "foo(a,b,c)")
>>> m.groups()
('foo', 'a', 'c')
But, what about something like this:
foo(a,b,c
d,e,f)
?? Yeah, it gets hard fast with regexes and you move on to richer parsing tools.