0

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?

ps0604
  • 1,227
  • 23
  • 133
  • 330
  • You seem to be capturing whitespace for the arguments. It also appears that this regex can capture just as list of commas as argument, since `(\w*)` would indicate even zero-length sequences (of whitespace). And the last argument for the function here requires to be followed by a comma; you may need to add a separate group for the last argument. –  Apr 12 '16 at 00:05
  • you can't use special characters within `[]`. Also, don't use regex for this –  Apr 12 '16 at 00:05
  • I doubt you can use grouping parenthesis inside a character set. –  Apr 12 '16 at 00:05
  • @hop: you can use special characters, but you likely can't use grouping. the parentheses just take a different value than intended, but the `\w` (a special character) does work. –  Apr 12 '16 at 00:06
  • @Evert: yeah, like splitting hairs will fix this mess. –  Apr 12 '16 at 00:10
  • I'm new to regex, but this doesn't seem a complex problem to solve, why is it? @hop it would be good if you made a recommendation instead of just giving negative comments – ps0604 Apr 12 '16 at 00:13
  • @ps0604: if you are new to regex, how do you know it's not a complex problem? here is a recommendation: don't use regex. seriously. –  Apr 12 '16 at 00:14

2 Answers2

1

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

Community
  • 1
  • 1
Ali Akbar Azizi
  • 3,272
  • 3
  • 25
  • 44
0

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.

Sean Perry
  • 3,776
  • 1
  • 19
  • 31