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?

7
  • 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. Commented Apr 12, 2016 at 0:05
  • you can't use special characters within []. Also, don't use regex for this Commented Apr 12, 2016 at 0:05
  • I doubt you can use grouping parenthesis inside a character set. Commented Apr 12, 2016 at 0: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. Commented Apr 12, 2016 at 0:06
  • @Evert: yeah, like splitting hairs will fix this mess. Commented Apr 12, 2016 at 0:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, your answer got me closer, see regex101.com/r/pX0nQ6/7
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.

4 Comments

there's a problem with your regex, see regex101.com/r/pX0nQ6/5
Yeah, the joys of trying to answer a question before heading home.
you can eliminate any new lines and extra spaces before you run the regex
Sometimes. Depends on syntax, comments, etc. If they embed comments after each parameter than just collapsing the newlines will not work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.