Sorry in advance for bad formatting ...
So I played around a bit more and ended up with something else - which
requires rather more rules. Am I overcomplicating it
Many thx in advance
Public Shared ReadOnly OneDigit As Parser(Of IEnumerable(Of
Char)) = Sprache.Parse.Digit.Once
Public Shared ReadOnly TwoDigits As Parser(Of IEnumerable(Of
Char)) = From a In OneDigit
From b In Sprache.Parse.Digit.Once
Select a.Concat(b)
Public Shared ReadOnly ThreeDigits As Parser(Of IEnumerable(Of
Char)) = From a In TwoDigits
From b In Sprache.Parse.Digit.Once
Select a.Concat(b)
Public Shared ReadOnly KeyIndexRange As Parser(Of
IEnumerable(Of Char)) = ThreeDigits.Or(TwoDigits).Or(OneDigit).End
Public Shared ReadOnly Key1 As Parser(Of IEnumerable(Of Char))
= From a In Sprache.Parse.Char("M"c).Once
From b In KeyIndexRange
Select a.Concat(b)
Public Shared ReadOnly Key2 As Parser(Of IEnumerable(Of Char))
= From a In Key1
From b In Sprache.Parse.Char("E"c).Once
From c In KeyIndexRange
Select a.Concat(b).Concat(c)
Public Shared ReadOnly Key3 As Parser(Of IEnumerable(Of Char))
= From a In Key2
From b In Sprache.Parse.Char("I"c).Once
From c In KeyIndexRange
Select a.Concat(b).Concat(c)
Public Shared ReadOnly DataItemKey As Parser(Of String) =
Key3.End.Text
> Hi
>
> Just getting into Sprache .. and loving it .. but am wondering if I
> can just check out my understanding
>
> I am wanting to parse a particular character pattern to identify a key
> in a string. The pattern can be
>
> from M1E1I1 up to M999E999I999
>
> I have created a parser for this as follows
>
> Public ReadOnly Key As Parser(Of String) =
> From a In Parse.Char("M"c).Once
> From b In Parse.Digit.AtLeastOnce
> From c In Parse.Char("E"c).Once
> From d In Parse.Digit.AtLeastOnce
> From e In Parse.Char("I"c).Once
> From f In Parse.Digit.AtLeastOnce
> Select New
> String(a.Concat(b).Concat(c).Concat(d).Concat(e).Concat(f).ToArray)
>
> but obviously it is not tight enough.
>
> I be grateful if you could just confirm am going in the right
> direction, or whether there is a better way to perform such a pattern
> match.
>
> Also, I was wondering whether infact it would be going against the
> grain to have a RegEx type parser/combinator
>
> eg something like
>
> Public ReadOnly Key As Parser(Of String) = Parse.Regex("^M[1-9][0-9]
> {0,2}E[1-9][0-9]{0,2}I[1-9][0-9]{0,2}").Once
>
> Though I'm not sure whether that could end up being self
> contradictory, eg any subsequent combinator may be contradicted by the
> content of the regex itself?
>
> Many thx again
>
> Simon