Generally, you'd need two steps to get all data.
First, match/validate the whole function:
function\((?<parameters>((\/\*[a-zA-Z]+\*\/)?[0-9a-zA-Z_$]+,?)*)\)
Note that now you have a parameters group with all parameters. You can match some of the pattern again to get all matches of parameters, or in this case, split on ,.
If you're using .Net, by any chance, you're in luck. .Net keeps full record of all captures of each group, so you can use the collection:
match.Groups["param"].Captures
Some notes:
- If you do want to capture more than one type, you definitely want empty matches, so you can easily combine the matches (though you can sort, but a 1-to-1 capture is neater). In that case, you want the optional group inside your captured group:
(?<type>(\/\*[a-zA-Z]+\*\/)?)
- You don't have to escape slashes in .Net patterns -
/ has no special meaning there (C#/.Net doesn't have regex delimiters).
Here's an example of using the captures. Again, the main point is maintaining the relation between type and param: you want to capture empty types, so you don't lose count.
Pattern:
function
\(
(?:
(?:
/\*(?<type>[a-zA-Z]+)\*/ # type within /* */
| # or
(?<type>) # capture an empty type.
)
(?<param>
[0-9a-zA-Z_$]+
)
(?:,|(?=\s*\))) # mandatory comma, unless before the last ')'
)*
\)
Code:
Match match = Regex.Match(s, pattern, RegexOptions.IgnorePatternWhitespace);
CaptureCollection types = match.Groups["type"].Captures;
CaptureCollection parameters = match.Groups["param"].Captures;
for (int i = 0; i < parameters.Count; i++)
{
string parameter = parameters[i].Value;
string type = types[i].Value;
if (String.IsNullOrEmpty(type))
type = "NO TYPE";
Console.WriteLine("Parameter: {0}, Type: {1}", parameter, type);
}