public List <CondGroup> Parse() { childCondStrings.Clear(); string escapeedCode = "AND " + Regex.Replace(code, @"\((.*?)\)", ReplaceByPlaceholder); Regex regex = new Regex(@"(AND|OR)\s+(\S+)\s+(\S+)\s+(\S+)\s*"); Regex placeHolderRegex = new Regex(@"^(AND|OR)\s+" + PLACEHOLDER); List <CondGroup> condGroups = new List <CondGroup>(); MatchCollection mc = regex.Matches(escapeedCode); int placeHolderIndex = 0; foreach (Match m in mc) { CondGroup condGroup = new CondGroup(); condGroups.Add(condGroup); condGroup.Connector = m.Groups[1].Value; if (placeHolderRegex.IsMatch(m.Groups[0].Value)) { string childCode = "OR " + childCondStrings[placeHolderIndex++]; MatchCollection childMatchCollection = regex.Matches(childCode); foreach (Match childMatch in childMatchCollection) { Cond cond = new Cond(); condGroup.Conds.Add(cond); cond.Connector = childMatch.Groups[1].Value; cond.LeftValue = childMatch.Groups[2].Value; cond.Operator = childMatch.Groups[3].Value; cond.RightValue = childMatch.Groups[4].Value; } } else { Cond cond = new Cond(); condGroup.Conds.Add(cond); cond.LeftValue = m.Groups[2].Value; cond.Operator = m.Groups[3].Value; cond.RightValue = m.Groups[4].Value; } } return(condGroups); }
public List<CondGroup> Parse() { childCondStrings.Clear(); string escapeedCode = "AND " + Regex.Replace(code, @"\((.*?)\)", ReplaceByPlaceholder); Regex regex = new Regex(@"(AND|OR)\s+(\S+)\s+(\S+)\s+(\S+)\s*"); Regex placeHolderRegex = new Regex(@"^(AND|OR)\s+" + PLACEHOLDER); List<CondGroup> condGroups = new List<CondGroup>(); MatchCollection mc = regex.Matches(escapeedCode); int placeHolderIndex = 0; foreach (Match m in mc) { CondGroup condGroup = new CondGroup(); condGroups.Add(condGroup); condGroup.Connector = m.Groups[1].Value; if (placeHolderRegex.IsMatch(m.Groups[0].Value)) { string childCode = "OR " + childCondStrings[placeHolderIndex++]; MatchCollection childMatchCollection = regex.Matches(childCode); foreach (Match childMatch in childMatchCollection) { Cond cond = new Cond(); condGroup.Conds.Add(cond); cond.Connector = childMatch.Groups[1].Value; cond.LeftValue = childMatch.Groups[2].Value; cond.Operator = childMatch.Groups[3].Value; cond.RightValue = childMatch.Groups[4].Value; } } else { Cond cond = new Cond(); condGroup.Conds.Add(cond); cond.LeftValue = m.Groups[2].Value; cond.Operator = m.Groups[3].Value; cond.RightValue = m.Groups[4].Value; } } return condGroups; }