I want to match strings where the first character is a letter, then it is followed by multiple characters which are either digits or letters, then finally ends with a letter. For example a11a11a is correct but a11aa11 is incorrect because it ends with a digit and not a letter.
I wrote the following code to do it:
var grammar =
from first in Parse.Letter.Once()
from rest in Parse.LetterOrDigit.Many()
from end in Parse.Letter.Once()
select new string(first.Concat(rest).Concat(end).ToArray());
var result = grammar.TryParse("a111a");
Unfortunately LetterOrDigit.Many() consumes the last letter too.
Any way to avoid this?
Here is a solution:
The clauses in the
Or's need to be in the correct order.A commenter recommended the use of Regular Expressions. You can use them within Sprache: