This is a case where I think Regular Expressions make a lot of sense. Unlike C++, C# does not have a way (that I know of) to use string formatting as part of parsing a string.
Quoting Eric Lippert:
Is this clever? No. Beautiful? No.
Short? No. Correct according to the
specification?
I hope so, but I have not fully tested it. It looks pretty good though.
static bool testJoin(string x)
{
string[] s = x.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (s.Length != 3) return false;
if (s[1] != "+" && s[1] != "-") return false;
if (s[0] != "x") return false;
double tmp;
return Double.TryParse(s[2], out tmp);
}
static bool testString(string x)
{
if (x.Length < 2) return false;
if (x[0] != '(' || x[x.Length-1]!=')') return false;
string[] y = x.Substring(1,x.Length-2).Split(new string[] { ")(" }, StringSplitOptions.None);
if (y.Length != 2) return false;
return testJoin(y[0]) && testJoin(y[1]);
}