2011-05-20 66 views
0

我需要驗證的邏輯表達式一樣,如何驗證邏輯表達式使用正則表達式或C#

((var1 == var2) && (var2 >= var4 || var1 > var5)) 

(var1 < var2) 

(var1 == var2 || var3 != var4) 

每一個括號,變量和邏輯運算符是由單個空格隔開。

我需要一個正則表達式來解析它。

或告訴我一些邏輯來驗證這與C#。

謝謝。

+4

由於嵌套括號,它不是正則表達式的工作。我建議你從標題中刪除「使用正則表達式」,然後更多的c#人員會看你的問題。 – stema 2011-05-20 06:49:46

+0

是否必須使用正則表達式?你能使用像ANTLR這樣的第三方工具嗎? – 2011-05-20 06:50:47

回答

0

要麼建立你自己的解析器,或看下面(找到here)。您需要稍微改變一下以考慮變量,但這不應該太難。要麼在將它們傳遞給函數之前解析它們,要麼將函數更改爲除變量名稱和值以外的函數。

using System.CodeDom.Compiler; 
    using Microsoft.CSharp; 
    using System.Reflection; 
    /// <summary> 
    /// A simple function to get the result of a C# expression (basic and advanced math possible) 
    /// </summary> 
    /// <param name="command">String value containing an expression that can evaluate to a double.</param> 
    /// <returns>a Double value after evaluating the command string.</returns> 
    private double ProcessCommand(string command) 
    { 
     //Create a C# Code Provider 
     CSharpCodeProvider myCodeProvider = new CSharpCodeProvider(); 
     // Build the parameters for source compilation. 
     CompilerParameters cp = new CompilerParameters(); 
     cp.GenerateExecutable = false;//No need to make an EXE file here. 
     cp.GenerateInMemory = true; //But we do need one in memory. 
     cp.OutputAssembly = "TempModule"; //This is not necessary, however, if used repeatedly, causes the CLR to not need to 
              //load a new assembly each time the function is run. 
     //The below string is basically the shell of a C# program, that does nothing, but contains an 
     //Evaluate() method for our purposes. I realize this leaves the app open to injection attacks, 
     //But this is a simple demonstration. 
     string TempModuleSource = "namespace ns{" + 
            "using System;" + 
            "class class1{" + 
            "public static double Evaluate(){return " + command + ";}}} "; //Our actual Expression evaluator 

     CompilerResults cr = myCodeProvider.CompileAssemblyFromSource(cp,TempModuleSource); 
     if (cr.Errors.Count > 0) 
     { 
      //If a compiler error is generated, we will throw an exception because 
      //the syntax was wrong - again, this is left up to the implementer to verify syntax before 
      //calling the function. The calling code could trap this in a try loop, and notify a user 
      //the command was not understood, for example. 
      throw new ArgumentException("Expression cannot be evaluated, please use a valid C# expression"); 
     } 
     else 
     { 
      MethodInfo Methinfo = cr.CompiledAssembly.GetType("ns.class1").GetMethod("Evaluate"); 
      return (double)Methinfo.Invoke(null, null); 
     } 
    } 
+0

@GeorgeDuckett你測試了嗎? – 2011-05-20 06:53:58

+0

不,從查看原始線程,至少需要一次編輯,將進行測試和更新。 – 2011-05-20 06:56:04

+0

剛剛測試過,對我無影響,沒有任何變化。 – 2011-05-20 06:59:01

0

如果我是這樣做的,我想要做它作爲一個正則表達式,我將創建每次通過消除驗證子情形多次通過算法(例如更換驗證「(X == Y)」與「 X」

也許東西開始:s/\b\w+ $op \w+\b/^^MAGICTOKEN^^/g

那麼任何/ $ OP /將是非法的

然後在循環專注於每括號:s/\(([^\)]+)\)/^^MAGICTOKEN^^/

雖然專注於1美元,但減少:s/ $MAGICTOKRE $BOOL $MAGICTOKRE/^^MAGICTOKEN^^ / 在循環中運行減少直到它停止減少。 如果$1 ne " ^^MAGICTOKEN^^ ",錯誤

聚焦循環後,最有可能的$expression ne "^^MAGICTOKEN^^"將指示一個錯誤。

+0

我建議你將代碼替換爲C#,以便OP可以理解它;) – 2011-05-20 06:58:31