Alright, I’ve read the tutorials and scrambled my head too much to be able to see clearly now.
I’m trying to capture parameters and their type info from a function signature. So given a signature like this:
function(/*string*/a,b,c)
I want to get the parts like this:
type: string
param:a
param:b
param:c
This is Ok too:
type: string
param:a
type: null (or whitespace)
param:b
type: null (or whitespace)
param:c
So I came up with this regex which is doing the common mistake of repeating the capture (I’ve explicit capture turned on):
function\(((\/\*(?<type>[a-zA-Z]+)\*\/)?(?<param>[0-9a-zA-Z_$]+),?)*\)
Problem is, I can’t correct the mistake. :(. Please help!
Generally, you’d need two steps to get all data.
First, match/validate the whole function:
Note that now you have a
parametersgroup 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:
Some notes:
(?<type>(\/\*[a-zA-Z]+\*\/)?)/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
typeandparam: you want to capture empty types, so you don’t lose count.Pattern:
Code: