Q:

How to parse a specific function name and its parameters from string?

I'm trying to parse function name and its parameters to update the string contents. I'm storing function call in a string and before invoking it i need to modify it and then invoke. Following is string containing function.
var expression = "AreEqual  ( \"test\" ,  Obj.Prop ) && AreEqual ( 1 , 2 ) && AREeQuAl( Obj.Prop , 1 )&& AreEqual (\"\\\"\\\",\" , 2 ) AND AreEqual (',' , ',' ) AreEqual ( \"A,B\" , Obj.Prop ) ";

var expectedOutPut = "MyClass.AreEqual( new (\"test\" AS A) , new ( Obj.Prop AS A) ) && MyClass.AreEqual ( new( 1 AS A ), new ( 2 AS A) ) && MyClass.AREeQuAl( new (Obj.Prop AS A) , new ( 1 AS A) ) && MyClass.AreEqual (new ( \"\\\"\\\",\" AS A) , new ( 2 AS A)  ) && MyClass.AreEqual (new (',' AS A) , new( ',' AS A )) && MyClass.AreEqual ( new (\"A,B\" AS A) ,new ( Obj.Prop AS A) )";

I tried following regex but it's breaking in valid commas inside double quotes.
@"(AreEqual.*?\()\s*([^,]+?)\s*(?=,|$)"

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string pattern = @"(AreEqual.*?\()\s*([^,]+?)\s*(?=,|$)";
        string input = @"AreEqual  ( ""test"" ,  Obj.Prop ) && AreEqual ( 1 , 2 ) && AREeQuAl( Obj.Prop , 1 )&& AreEqual (""\""\"","" , 2 ) AND AreEqual (',' , ',' ) AreEqual ( ""A,B"" , Obj.Prop )";

        RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
        Console.ReadLine();
    }
}

A:

I tried to match the items into groups and then format a new string using those groups.
string pattern = @"(AreEqual)\s*\((\s*[\""']*[\w,\\]*(.\w+)*[\""']*\s*),(\s*[\""']*[\w,\\]*(.\w+)*[\""']*)\s*\)";
string input = @"AreEqual  ( ""test"" ,  Obj.Prop ) && AreEqual ( 1 , 2 ) && AREeQuAl( Obj.Prop , 1 )&& AreEqual (""\""\"","" , 2 ) AND AreEqual (',' , ',' ) AreEqual ( ""A,B"" , Obj.Prop )";

RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;

List<string> expectedOutputParts = new List<string>();
foreach (Match m in Regex.Matches(input, pattern, options))
{
    string newstring = $"MyClass.{m.Groups["1"]}( new ({m.Groups["2"]} AS A) , new ({m.Groups["4"]} AS A) )";
    expectedOutputParts.Add(newstring);         

}   

Console.WriteLine(string.Join(" && ", expectedOutputParts));

Output:

MyClass.AreEqual( new ( "test"  AS A) , new (  Obj.Prop AS A) ) && MyClass.AreEqual( new ( 1  AS A) , new ( 2 AS A) ) && MyClass.AREeQuAl( new ( Obj.Prop  AS A) , new ( 1 AS A) ) && MyClass.AreEqual( new (','  AS A) , new ( ',' AS A) ) && MyClass.AreEqual( new ( "A,B"  AS A) , new ( Obj.Prop AS A) )

Disclaimer:
this version does not contain the AreEqual (""\""\"","" , 2 ) part. I still haven't figured that out.

