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 fromxup to and includingyare allowed==['x']defines that only characterxis alloweda || bdefines that characters matching the specaor the specbare allowed