Ok, assuming you want it to simply split on logical and relational operators, you can use this pattern:
string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";
This will also trim all whitespace from the returned strings.
Code:
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();
}
}
}
Test:
https://ideone.com/DaRtP
Output:
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|
Edit
Ok, with the additional constraints, you should be able to use this:
string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";
This will still trim all whitespace from the returned strings. It will, however, return empty strings if matches are right next to each other (e.g. Name=='Stefan+123'). I was unable to work around that this time, but it's not so important.
If you import System.Linq and System.Collections.Generic and make the results a List<string>, you can remove all empty strings from the List in one extra line like this (which is slower than using straight-up for loops):
var results = Regex.Split(input.Trim(), pattern).ToList();
results.RemoveAll(x => x == "");
Code:
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();
}
}
}
Test:
https://ideone.com/r5lpE
Output:
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'
|
Additional Comments:
If you want to split on any other operators (e.g., <<, +=,=, -=, >>) as well (there's a lot), or need anything else, just ask.