Swiftpack.co - elegantchaos/Expressions as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by elegantchaos.
elegantchaos/Expressions v1.1.1
Swift regular expression capture support.
⭐️ 3
🕓 1 year ago
iOS macOS tvOS
.package(url: "https://github.com/elegantchaos/Expressions.git", from: "v1.1.1")

Test results Latest release swift 5.0 shield swift 5.1 shield swift 5.2 shield swift 5.3 shield swift dev shield Platforms: macOS, iOS, tvOS, watchOS, Linux

Expressions

Some utilities to make it a little easier to work with regular expressions in Swift when they have capture groups in them.

Positional Captures

Let's say we have a regular expression (\w+) (.*) (\w+), and a structure that we want to unpack expression matches into:

class Result {
var first = ""
var last = ""
var number = 0
}

We can match an expression and capture the results like this:

let pattern = try! NSRegularExpression(pattern: "(\\w+) (.*) (\\w+)", options: [])
var result = Result()
if pattern.firstMatch(in: "Sam 123 Deane", capturing: [\Result.first: 1, \Result.last: 3, \Result.number: 2], into: &result) {
    // result now contains the captured parameters
}

Note that Result here can be a class or a structure.

Named Captures

For an even cleaner mapping, we can also use named captures.

Given an expression

(?xi)
(?<first>   \w+ ) ?(?-x: )
(?<number>  .*  ) ?(?-x: )
(?<last>    \w+ )

and a results structure:

class Result: NSObject {
    @objc var first = ""
    @objc var last = ""
    @objc var number = 0
}

We can match an expression and capture the results like this:

var result = Result()
if namedCapturePattern.firstMatch(in: "Sam 123 Deane", capturing: &result) {
    // result now contains the captured parameters
}

Note that the implementation relies on key-value support to write the results, so that Result instance has to be an Objective-C class, as do any named properties to be captured.

This is a limitation of Swift reflection, which currently only supports reading values.

Work In Progress

This is a bit of a sketch at the moment.

It needs fleshing out with variations that return lists of matches.

With some improvements to Swift reflection, the code could be simplified somewhat, and the ugly requirement for Obj-C inheritance removed.

GitHub

link
Stars: 3
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.1.1
3 years ago

Fixed API visibility.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics