Section 3 - Character Classes
Looking at the
Twitter spec
more closely, digits and _
characters are also allowed in the username
portion of the handle. A simple Range
will not be sufficient here, so
we can turn to CharacterClass
and it’s associated Spec
types.
import ropes.core._
import ropes.core.Spec._
type Username = Repeated[1, 15, CharacterClass[('a' - 'z') || ('A' - 'Z') || ('0' - '9') || ==['_']]]
type TwitterHandle = Literal['@'] Concat Username
To keep things concise, it uses symbolic definitions, so let’s at these in turn:
'x' - 'y'
defines that any characters fromx
up to and includingy
are allowed==['x']
defines that only characterx
is alloweda || b
defines that characters matching the speca
or the specb
are allowed