using System.Text.RegularExpressions;
using System.Collections.Generic;
using CodingSeb.ExpressionEvaluator;
public class XExpressionEvaluator : ExpressionEvaluator
protected override void Init()
ParsingMethods.Insert(0, EvaluateDateTimeSyntax);
ParsingMethods.Add(EvaluateSpecialTernaryOperator);
protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
Match match = Regex.Match(expression.Substring(i), @"^\s*#(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})");
int year = int.Parse(match.Groups["year"].Value);
int month = int.Parse(match.Groups["month"].Value);
int day = int.Parse(match.Groups["day"].Value);
DateTime dateTime = new DateTime(year,month, day);
protected virtual bool EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
if (expression.Substring(i, 1).Equals("°"))
string input = (string)ProcessStack(stack);
string restOfExpression = expression.Substring(i + 1);
for (int j = 0; j < restOfExpression.Length; j++)
string s2 = restOfExpression.Substring(j, 1);
Match internalStringMatch = stringBeginningRegex.Match(restOfExpression.Substring(j));
if (internalStringMatch.Success)
string innerString = internalStringMatch.Value + GetCodeUntilEndOfString(restOfExpression.Substring(j + internalStringMatch.Length), internalStringMatch);
j += innerString.Length - 1;
GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(restOfExpression, ref j, false);
stack.Push(input.Replace(Evaluate<string>(restOfExpression.Substring(1, j - 1)), Evaluate<string>(restOfExpression.Substring(j + 1))));
public static void Main()
ExpressionEvaluator evaluator = new XExpressionEvaluator();
List<string> expressions = new List<string>()
"\"A sentence where a word must be replaced where it is\" ° \"replaced\" @ \"kept\"",
"#1985-09-11.Equals(new DateTime(1985,9,11))"
expressions.ForEach(expression =>
Console.WriteLine(expression);
Console.WriteLine(evaluator.Evaluate(expression));
catch(Exception exception)
Console.WriteLine(exception.Message);
Console.WriteLine(string.Empty);