I need to search for all occurrences of chained C#-like functions in a text string. For example, I would like to break out each method and its parenthetical arguments of a string such as this:
object.method(1, "2", abc).method2().method3("test(), 1, 2, 3").method4("\"Hi\"")
Here is the regex pattern I almost had working:
(?<objectName>[^\}]*?)\.(?<methodName>[^\}]*?)\(((?:[^;"']|"[^"]*"|'[^']*')+)*?\)
This extracts the objectName and the first methodName correctly, but lumps
1, "2", abc).method2().method3("test, 1, 2, 3").method4("\"Hi\""
all into the third argument as "$1".
My latest approach was to divide and conquer by removing the objectName specification as that is easy to parse out. This lead me to using:
\.(?<methodName>[^(]*?)\(((?:[^;"']|"[^"]*"|'[^']*')+)*?\)
Which yields similar results as before obviously without the objectName. I did this to see if I could get a global result but could get the right regex syntax.
In summary, I need to parse out multiple chained .method(parameters) occurrences into their constituent parts named "methodName" and "parameters". I have deduced a few things but my regex skills are quite rusty at best and am unable to overcome this at this time. I appreciate any help you may have to offer.
I have been using this site for testing: http://regexstorm.net/tester
UPDATE: To clarify, the requirements do not include supporting C# lambda expressions, only the dotted function syntax. This is not intended to be a full C# parser. The only need is the dotted method chaining. I apologize for any confusion. The pattern I was looking to breakout is:
object.method(arguments).method(arguments).method(arguments)...
My approach to this was to first extract the object name which is a simple operation that does not require the use of Regex. This would now leave the following for Regex parsing into two constituent parts:
.method(arguments).method(arguments).method(arguments)...
Which would yield:
method arguments
method arguments
method arguments
...
arguments may be null (missing), as in .method(), or method may actually be a property (no parentheses and arguments), as in:
.method.method().method(arguments)
Which would yield:
method (null)
method (string.Empty)
method arguments
arguments would contain everything between the opening and closing parentheses; these do not need to be parsed out at this time as those would be processed in a subsequent Regex operation.
This seems to me to be within the capability of Regex to detect this simple pattern of dot-method-openPar-argumentsStr-closePar next dot-method-openPar-argumentsStr-closePar and so forth.
This is the extent of the grammar - no comments, no lambda - just object.method(arguments).method()...
I hope this helps.