如何从字符串中解析特定的函数名称及其参数?

时间:2019-04-12 04:41:53

标签: c# regex regex-group

我正在尝试解析函数名称及其参数以更新字符串内容。我将函数调用存储在字符串中,在调用它之前,我需要对其进行修改然后再调用。以下是包含函数的字符串。

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) )";

我尝试使用正则表达式,但是它在双引号内打了个逗号。

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

2 个答案:

答案 0 :(得分:2)

我尝试将项目匹配到组中,然后使用这些组格式化新字符串。

 val inputFormat = new SimpleDateFormat("yyyy-MM-dd")
 val outPath = "maindir/sb_topic/data/loaddate="

val dswWriteStream =dfresult.writeStream
    .outputMode(outputMode) 
    .format(writeformat) 
    .option("path",outPath+inputFormat.format((new java.util.Date()).getTime())) //hdfs file write path
    .option("checkpointLocation", checkpointdir) 
    .option("maxRecordsPerFile", 999999999) 
    .trigger(Trigger.ProcessingTime("10 minutes")) 

输出:

  

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

免责声明:

此版本不包含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) )部分。我仍然没有弄清楚。

答案 1 :(得分:0)

这是通用解决方案:

PostGreSQL

输出结果。