RegexDefinitions
struct RegexDefinitions
Regex definitions to parse a chord
-
The regex for a
chord
stringIt will parse the chord to find the
root
and optionalquality
/// ## Examples Am -> root: Am, quality: m Dsus4 -> root: D, quality: sus4
Declaration
Swift
let chordRegex = Regex { /// The root rootRegex /// The optional quality Optionally { Capture { OneOrMore { CharacterClass( (.word), (.digit), .anyOf("#?") ) } } transform: { quality in /// Try to find the name of the quality for name in Chord.Quality.allCases where name.name.contains(String(quality)) { return name } /// The quality is unknown return Chord.Quality.unknown } } Optionally { "/" rootRegex } }
-
The regex for a chord definition
Declaration
Swift
let defineRegex = Regex { /// Capture the name Capture { OneOrMore { CharacterClass( .anyOf("#b/+?"), (.word), (.digit) ) } } transform: { name in String(name) } /// Capture the base-fret Optionally { " base-fret " Capture { OneOrMore(.digit) } transform: { baseFret in Int(baseFret) ?? 0 } } /// Capture the frets Optionally { " frets " Capture { OneOrMore { CharacterClass( .anyOf("x"), (.digit), (.whitespace) ) } } transform: { frets in String(frets).trimmingCharacters(in: .whitespacesAndNewlines) } } /// Capture the fingers Optionally { "fingers " Capture { OneOrMore { CharacterClass( (.digit), (.whitespace) ) } } transform: { fingers in return String(fingers).trimmingCharacters(in: .whitespacesAndNewlines) } } }