Check syntax (string and character comparison) I would like to know if my function that checks if a string syntax is correct. It's currently very messy, but seems to do the job.

//Example Valid String
[12345678]
{
something
whatever
}


Function:

private static bool CheckIfSyntaxIsValid(string input)
        {
            int mode = 1;
            char last = ' ';
            foreach (char c in input)
            {
                if (last == '{' &amp;&amp; c == '}')
                {
                    last = ' ';
                    mode = 1;
                    continue;
                }
                if (last == '{' &amp;&amp; c == '{')
                    return false;

                if (last == '[' &amp;&amp; c == ']')
                {
                    last = ' ';
                    mode = 2;
                    continue;
                }
                if (last == '[' &amp;&amp; !Char.IsDigit(c))
                    return false;

                if (c == '[')
                {
                    if (mode == 2)
                        return false;
                    last = c;
                    mode = 1;
                }
                if (c == '{')
                {
                    if (mode == 1)
                        return false;

                    last = c;
                    mode = 2;
                }

                if (c == ']' || c == '}')
                    return false;
            }

            if (last != ' ')
                return false;

            return true;

        }


The rule is basically: 


[] must contain only numbers/digits
It must start with [] before {}, and they must come after each other


So, [] {} [] {} etc. Anything else is simply invalid.

Okay, not everything else is invalid. Text outside of those are ignored,
so the only thing that matters is that the bracket orders are correct, etc.
I suggest making the code more expressive by encapsulating the conditions as lambdas before the loop.

You should avoid magic numbers and for the mode create constants or an enum.

In the following example the {} stand for a block and [] for an attribute.

private static bool CheckIfSyntaxIsValid(string input)
{
    var mode = 1;
    var last = ' ';

    var isEndOfEmptyBlock = new Func&lt;char, bool&gt;(c =&gt; last == '{' &amp;&amp; c == '}');
    var isBeginOfNestedBlock = new Func&lt;char, bool&gt;(c =&gt; last == '{' &amp;&amp; c == '{');
    var isEndOfEmptyAttribute = new Func&lt;char, bool&gt;(c =&gt; last == '[' &amp;&amp; c == ']');
    var isNotDigitInsideAttribute = new Func&lt;char, bool&gt;(c =&gt; last == '[' &amp;&amp; !Char.IsDigit(c));
    var isAttributeBegin = new Func&lt;char, bool&gt;(c =&gt; c == '[');
    var isAttributeEnd = new Func&lt;char, bool&gt;(c =&gt; c == ']');
    var isBlockBegin = new Func&lt;char, bool&gt;(c =&gt; c == '{');
    var isBlockEnd = new Func&lt;char, bool&gt;(c =&gt; c == '}');

    foreach (char c in input)
    {
        if (isEndOfEmptyBlock(c))
        {
            last = ' ';
            mode = 1;
            continue;
        }

        if (isBeginOfNestedBlock(c))
        {
            return false;
        }

        if (isEndOfEmptyAttribute(c))
        {
            last = ' ';
            mode = 2;
            continue;
        }

        if (isNotDigitInsideAttribute(c))
        {
            return false;
        }

        if (isAttributeBegin(c))
        {
            if (mode == 2)
            {
                return false;
            }
            last = c;
            mode = 1;
        }

        if (isBlockBegin(c))
        {
            if (mode == 1)
            {
                return false;
            }

            last = c;
            mode = 2;
        }

        if (isAttributeEnd(c) || isBlockEnd(c))
        {
            return false;
        }
    }

    if (last != ' ')
    {
        return false;
    }

    return true;
}