【问题标题】:Workflow Foundation: C# parse VB expression from string工作流基础:C# 从字符串中解析 VB 表达式
【发布时间】:2016-12-29 23:10:29
【问题描述】:

我需要从以下字符串计算 VB 表达式:

"stringVar1  =   \"stringVar1Value\"    and boolVar1<>False or intVar1=22 and    intVar2=33"

"stringVar1=  \"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33"

"stringVar1  =   \"stringVar1Value\"    and boolVar1<>False or intVar1=22 and    intVar2=33"

我需要将其解析为类的变量数组

 public class ExpressionUnit
    {
        public string Variable { get; set; }
        public string Operator { get; set; }
        public string Value { get; set; }
    }

其中Variable 是“stringVar1”,Operator 是“=”,Value 是“stringVar1Value”。 或严格顺序的字符串数组:

{ "stringVar1", "=", "stringVar1Value", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" }

我会采纳任何建议或想法。

【问题讨论】:

标签: c# vb.net algorithm


【解决方案1】:

我找到了一些解决方案:

 public static class StringParser
{
    public static List<string> ParseExpression(string expression)
    {
        //expression = System.Text.RegularExpressions.Regex.Replace(expression, @"\s+", " ");

        string word = string.Empty;
        int i = 0;
        List<string> list = new List<string>();
        while (i < expression.Length)
        {
            if (expression[i] == ' ')
            {
                if (!string.IsNullOrEmpty(word))
                {
                    list.Add(word);
                    word = string.Empty;
                }
                i++;
                continue;
            }
            if (expression[i] == '=')
            {
                if (!string.IsNullOrEmpty(word))
                {
                    list.Add(word);
                }
                word = new string(expression[i], 1);
                list.Add(word);
                word = string.Empty;
                i++;
                continue;
            }
            if (expression[i] == '<')
            {
                if (!string.IsNullOrEmpty(word))
                {
                    list.Add(word);
                }
                word = new string(expression[i], 1);
                i++;
                word += expression[i];
                list.Add(word);
                word = string.Empty;
                i++;
                continue;
            }

            word += expression[i];
            i++;
            if (!string.IsNullOrEmpty(word) && i == expression.Length)
            {
                list.Add(word);
            }
        }

        return list;
    }
}

单元测试证明它有效:

 [TestFixture]
public class VbExpressionParserTests
{
    [Test]
    public void VbExpressionParserTests_ParseExpression_String_List1()
    {
        string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
        string[] res = StringParser.ParseExpression("stringVar1  =   \"stringVar1Value\"    and boolVar1<>False or intVar1=22 and    intVar2=33").ToArray();
        CollectionAssert.AreEqual(correctResult, res);
    }
    [Test]
    public void VbExpressionParserTests_ParseExpression_String_List2()
    {
        string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
        string[] res = StringParser.ParseExpression("stringVar1=  \"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33").ToArray();
        CollectionAssert.AreEqual(correctResult, res);
    }
    [Test]
    public void VbExpressionParserTests_ParseExpression_String_List3()
    {
        string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
        string[] res = StringParser.ParseExpression("stringVar1  =   \"stringVar1Value\"    and boolVar1<>False or intVar1=22 and    intVar2=33").ToArray();
        CollectionAssert.AreEqual(correctResult, res);
    }

    [Test]
    public void VbExpressionParserTests_ParseExpression_String_IdealExpression()
    {
        string[] correctResult = { "stringVar1", "=", "\"stringVar1Value\"", "and", "boolVar1", "<>", "False", "or", "intVar1", "=", "22", "and", "intVar2", "=", "33" };
        string[] res = StringParser.ParseExpression("stringVar1=\"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33").ToArray();
        CollectionAssert.AreEqual(correctResult, res);
    }
}

【讨论】:

    【解决方案2】:
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    
    namespace MrB
    {
        class Program
        {
            static void Main()
            {
                string s1 = "stringVar1  =   \"stringVar1Value\"    and boolVar1<>False or intVar1=22 and    intVar2=33";
                string s2 = "stringVar1=  \"stringVar1Value\" and boolVar1<>False or intVar1=22 and intVar2=33";
                string s3 = "stringVar1  =   \"stringVar1Value\"    and boolVar1<>False or intVar1=22 and    intVar2=33";
    
                var rgx = new Regex("=");
                var replacement = " = ";
    
                string s1b = rgx.Replace(s1, replacement);
                string s2b = rgx.Replace(s2, replacement);
                string s3b = rgx.Replace(s3, replacement);
    
                rgx = new Regex(@"\s+");
                replacement = " ";
    
                string s1c = rgx.Replace(s1b, replacement);
                string s2c = rgx.Replace(s2b, replacement);
                string s3c = rgx.Replace(s3b, replacement);
    
                string[] s1List = s1c.Split(' ');
                string[] s2List = s2c.Split(' ');
                string[] s3List = s3c.Split(' ');
    
                Debugger.Break();
            }
        }
    }
    

    【讨论】:

    • 即1) 找到所有等号并替换为任一侧的空格 2) 用单个空格替换所有多个空格 3) 分割空格 - 给出 13 个元素
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2021-10-05
    • 1970-01-01
    相关资源
    最近更新 更多