using System; using System.Text.RegularExpressions; public class Test { public static void Main() { var inputTests = new[] { "5*-3", "(5--5)-3", "(5-------5)-3", "-(1-2)-3" }; foreach (var line in inputTests) { Console.WriteLine(line + " -> " + RemoveUnaryOperators(line)); } } private static Regex _regex = new Regex(@"(?<=^|[-(+*/])-(?\d+|\((?:[^\(\)]|(?\()|(?<-open>\)))+?(?(open)(?!))\))", RegexOptions.Compiled); private static string RemoveUnaryOperators(string input) { var result = Regex.Replace(input ?? string.Empty, @"\s+", string.Empty); string tmp; do { tmp = result; result = _regex.Replace(result, @"(0-${value})"); } while (result != tmp); return result; } }