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(@"(?<=^|[-(+*/])-(?<value>\d+|\((?:[^\(\)]|(?<open>\()|(?<-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;
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dC5SZWd1bGFyRXhwcmVzc2lvbnM7CgpwdWJsaWMgY2xhc3MgVGVzdAp7CglwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCgl7CgkJdmFyIGlucHV0VGVzdHMgPSBuZXdbXQoJCQl7CgkJCQkiNSotMyIsCgkJCQkiKDUtLTUpLTMiLAoJCQkJIig1LS0tLS0tLTUpLTMiLAoJCQkJIi0oMS0yKS0zIgoJCQl9OwoJCQoJCWZvcmVhY2ggKHZhciBsaW5lIGluIGlucHV0VGVzdHMpCgkJewoJCQlDb25zb2xlLldyaXRlTGluZShsaW5lICsgIiAgLT4gICIgKyBSZW1vdmVVbmFyeU9wZXJhdG9ycyhsaW5lKSk7CgkJfQoJfQoJCglwcml2YXRlIHN0YXRpYyBSZWdleCBfcmVnZXggPSBuZXcgUmVnZXgoQCIoPzw9XnxbLSgrKi9dKS0oPzx2YWx1ZT5cZCt8XCgoPzpbXlwoXCldfCg/PG9wZW4+XCgpfCg/PC1vcGVuPlwpKSkrPyg/KG9wZW4pKD8hKSlcKSkiLCBSZWdleE9wdGlvbnMuQ29tcGlsZWQpOwoJCglwcml2YXRlIHN0YXRpYyBzdHJpbmcgUmVtb3ZlVW5hcnlPcGVyYXRvcnMoc3Ry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