好的,假设您希望它简单地拆分为逻辑和关系运算符,您可以使用以下模式:
string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";    
这还将从返回的字符串中删除所有空格。
代码:
using System;
using System.Text.RegularExpressions;
namespace RegEx
{
    class Program
    {
        static void Main(string[] args)
        {
            string original = "([+\\-*/%()]{1}|[=<>!]{1,2}|[&|]{2})";
            string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";
            string input = "Name=='mynme' && CurrentTime<45 - 4";
            string input1 = "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'";
            string ridiculous = "Name == BLAH && !@#>=$%^&*()< ASDF &&    this          >          that";
            executePattern("original", input, original);
            executePattern("lordcheeto's", input, lordcheeto);
            executePattern("original", input1, original);
            executePattern("lordcheeto's", input1, lordcheeto);
            executePattern("original", ridiculous, original);
            executePattern("lordcheeto's", ridiculous, lordcheeto);
        }
        static void executePattern(string version, string input, string pattern)
        {
            // Avoiding repitition for this example.
            Console.WriteLine("Using {0} pattern:", version);
            // Needs to be trimmed.
            var result = Regex.Split(input.Trim(), pattern);
            // Pipes included to highlight whitespace trimming.
            foreach (var m in result)
                Console.WriteLine("|{0}|", m);
            // Extra space.
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}
测试:
http://goo.gl/XAm6J
输出:
Using original pattern:
|Name|
|==|
|'mynme' |
|&&|
| CurrentTime|
|<|
|45 |
|-|
| 4|
Using lordcheeto's pattern:
|Name|
|==|
|'mynme'|
|&&|
|CurrentTime|
|<|
|45 - 4|
Using original pattern:
|Name|
|==|
|'mynme' |
|&&|
| CurrentTime|
|<|
|'2012|
|-|
|04|
|-|
|20 19:45:45'|
Using lordcheeto's pattern:
|Name|
|==|
|'mynme'|
|&&|
|CurrentTime|
|<|
|'2012-04-20 19:45:45'|
Using original pattern:
|Name |
|==|
| BLAH |
|&&|
| |
|!|
|@#|
|>=|
|$|
|%|
|^&|
|*|
||
|(|
||
|)|
||
|<|
| ASDF |
|&&|
|    this          |
|>|
|          that|
Using lordcheeto's pattern:
|Name|
|==|
|BLAH|
|&&|
|!@#|
|>=|
|$%^&*()|
|<|
|ASDF|
|&&|
|this|
|>|
|that|
编辑
好的,有了额外的约束,你应该可以使用这个:
string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";
这仍然会从返回的字符串中删除所有空格。但是,如果匹配项彼此相邻(例如Name=='Stefan+123'),它将返回空字符串。这次我无法解决这个问题,但这并不重要。
如果您导入System.LinqandSystem.Collections.Generic并使结果为 a List<string>,您可以像这样从额外的一行中删除所有空字符串List(这比使用直接 for 循环要慢):
var results = Regex.Split(input.Trim(), pattern).ToList();
results.RemoveAll(x => x == "");
代码:
using System;
using System.Text.RegularExpressions;
namespace RegEx
{
    class Program
    {
        static void Main(string[] args)
        {
            string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";
            string input = "Name=='mynme' && CurrentTime<45 - 4";
            string input1 = "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'";
            string input2 = "((1==2) && 2-1==1) || 3+1==4 && Name=='Stefan+123'";
            executePattern("lordcheeto's", input, lordcheeto);
            executePattern("lordcheeto's", input1, lordcheeto);
            executePattern("lordcheeto's", input2, lordcheeto);
            Console.ReadLine();
        }
        static void executePattern(string version, string input, string pattern)
        {
            // Avoiding repitition for this example.
            Console.WriteLine("Using {0} pattern:", version);
            // Needs to be trimmed.
            var result = Regex.Split(input.Trim(), pattern);
            // Pipe included to highlight empty strings.
            foreach (var m in result)
                Console.WriteLine("|{0}", m);
            // Extra space.
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}
测试:
http://goo.gl/lkaoM
输出:
Using lordcheeto's pattern:
|Name
|==
|
|'mynme'
|
|&&
|CurrentTime
|<
|45
|-
|4
Using lordcheeto's pattern:
|Name
|==
|
|'mynme'
|
|&&
|CurrentTime
|<
|
|'2012-04-20 19:45:45'
|
Using lordcheeto's pattern:
|
|(
|
|(
|1
|==
|2
|)
|
|&&
|2
|-
|1
|==
|1
|)
|
|||
|3
|+
|1
|==
|4
|&&
|Name
|==
|
|'Stefan+123'
|
补充评论:
如果您还想拆分任何其他运算符(例如 , <<, +=, =, -=)>>(有很多),或者需要其他任何东西,请询问。