简体   繁体   中英

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();
    }
}

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.

here is a generic solution:

        var texte = "AreEqual  ( \"test\" ,  Obj.Prop ) && AreEqual ( 1 , 2 ) && AREeQuAl( Obj.Prop , 1 )&& AreEqual (\",\" , 2 ) AND AreEqual (',' , ',' ) AreEqual(\"A,B\", Obj.Prop)";

        //Extract function
        MatchCollection matches = Regex.Matches(texte, @".+?(?=\()");
        var function = Regex.Matches(texte, @".+?(?=\()")[0].ToString().Trim();


        var patternARGS = @"(?<=\().+? (?=\))";
        var patternExtractARGS = @"""[^, ]* , [^, ]*""( , )""[^, ]* , [^,]*""|[^, ]* , [^, ]*""( , )[^""]+""|[^""]+( , )""[^,]* , [^,]*""|( , )";

        // extract all arg between parenthesis
        matches = Regex.Matches(texte, patternARGS);

        //extract all args from previous result, with the difficulty to identify the right ','
        List<String> args = new List<String>();
        foreach (Match m in matches)
        {
            System.Diagnostics.Debug.WriteLine($"{m}");
            MatchCollection x = Regex.Matches(m.ToString(),patternExtractARGS);
            GroupCollection commas = x[0].Groups;

            var index = (commas.SyncRoot as Match).Index;
            var len = (commas.SyncRoot as Match).Length;
            var a1 = m.ToString().Substring(0, index);
            var a2 = m.ToString().Substring(index + len - 1);
            args.Add($"MyClass.{ function}( new ({a1}), new ({a2}))");
        }


        //extract conditions && AND...)
        var patternCONDITION = @"(?<=\)).+?(?=(?i: " + function + "))";
        matches = Regex.Matches(texte, patternCONDITION);



        var output = args[0];
        for(int i = 1;i<args.Count;i++)
        {
            output = output + $" {matches[i - 1].ToString().Trim()} {args[i]}";
        }

result in output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM