Suppose I had the string "1 AND 2 AND 3 OR 4", and want to create an array of strings that contains all substrings "AND" or "OR", in order, found within the string.
So the above string would return a string array of {"AND", "AND", "OR"}.
What would be a smart way of writing that?
EDIT: Using C# 2.0+,
string rule = "1 AND 2 AND 3 OR 4";
string pattern = "(AND|OR)";
string[] conditions = Regex.Split(rule, pattern);
gives me {"1", "AND", "2", "AND", "3", "OR", "4"}, which isn't quite what I'm after. How can I reduce that to the ANDs and ORs only?
Splitapproach is most appropriate for what you want. See the Split is separating the input at the ANDs and ORs thus resulting in the numbers (and only the AND/ORs coz of the parenthesis) - which is not what you want. You want the ANDs and ORs. I think a crafted regex pattern could return multiple matches thus capturing only the AND and ORs.