Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 2.25 KB

README.md

File metadata and controls

66 lines (49 loc) · 2.25 KB

swift-regex

Swift Regex is a Swift package that consists of a set of tools developed to work with Apple's RegexBuilder framework more easily.

Table of content

Motivation

This repository originated from experimentation with RegexBuilder during the parsing of inputs for Advent of Code problems. After using the framework for some time, I observed its complexity and verbosity relative to alternatives like Pointfree's swift-parsing. In fact, Pointfree has an episode that provides a comparison of these parsing techniques, which I recommend watching. Consequently, I decided to start this repository to incorporate helpers and custom components aimed at enhancing the ease and usability of RegexBuilder.

Components

List

The List component directly matches the number of occurrences for a given component into an array.

Imagine we want to parse some team data mapping its available and injured players:

let data = """
1: Bob, Alice | John, Molly
"""

struct Team {
    let id: Int
    let availablePlayers: [String]
    let injuredPlayers: [String]
}

Parsing the inner name lists would mean to perform a first match capturing them as strings and then perform a second match to parse the players out of them. Using the List component it can be directly done since its output is an actual array.

The regex expression representing the data is the following:

let list = List(
   OneOrMore(.word),
   separator: ", "
)
        
let regex = Regex {
    TryCapture(One(.digit)) { Int($0) }
    ": "
    Capture(list) { $0.map(String.init) }
    " | "
    Capture(list) { $0.map(String.init) }
}

And we could match the string using the regular matching API:

let team = data
    .wholeMatch(of: regex)
    .map { ($0.output.1, $0.output.2, $0.output.3) }
    .map(Team.init)