Swiftpack.co - TheAngryDarling/SwiftRegEx as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by TheAngryDarling.
TheAngryDarling/SwiftRegEx 1.1.0
A swift wrapper around NSRegularExpression. This package simplifies the use of regular expression with the conversion of NS objects to Swift objects
⭐️ 0
🕓 3 years ago
.package(url: "https://github.com/TheAngryDarling/SwiftRegEx.git", from: "1.1.0")

Regular Expression

swift >= 4.0 macOS Linux Apache 2.0

A swift wrapper around NSRegularExpression. This package simplifies the use of regular expression with the conversion of NS objects to Swift objects

This class also provides cross platform support for group names from Swift 4.0 and above, where as the NSTextCheckingResult only supports it on Apple platforms with newer OS's (MacOS 13, iOS 11, watchOS 4)

Usage

Full way of matching patterns

import RegEx

let pattern: String = "..."
let string: String = "..."
let regEx = try RegEx(pattern: pattern)
let matches = regEx.matches(in: string)
for match in matches {
    // let value = match.value
    // let range = match.range
    // let groupRange: Range<String.Index>? = match.range(at: 0)
    // let groupRangeByName: Range<String.Index>? = match.range(withName: "GroupName")
    // let groupValue = match.value(at: 0)
    // let groupValueByName = match.value(withName: "GroupName")
}

Get first match

import RegEx

let pattern: String = "..."
let string: String = "..."
let regEx = try RegEx(pattern: pattern)
if let match = regEx.firstMatch(in: string) {
    // let value = match.value
    // let range = match.range
    // let groupRange: Range<String.Index>? = match.range(at: 0)
    // let groupRangeByName: Range<String.Index>? = match.range(withName: "GroupName")
    // let groupValue = match.value(at: 0)
    // let groupValueByName = match.value(withName: "GroupName")
}

Directly from String

import RegEx

let pattern: String = "..."
let string: String = "..."
let matches = try string.match(pattern: pattern)
for match in matches {
    // let value = match.value
    // let range = match.range
    // let groupRange: Range<String.Index>? = match.range(at: 0)
    // let groupRangeByName: Range<String.Index>? = match.range(withName: "GroupName")
    // let groupValue = match.value(at: 0)
    // let groupValueByName = match.value(withName: "GroupName")
}

if let match = try string.firstMatch(pattern: pattern) {
    // let value = match.value
    // let range = match.range
    // let groupRange: Range<String.Index>? = match.range(at: 0)
    // let groupRangeByName: Range<String.Index>? = match.range(withName: "GroupName")
    // let groupValue = match.value(at: 0)
    // let groupValueByName = match.value(withName: "GroupName")
}

Author

License

This project is licensed under Apache License v2.0 - see the LICENSE.md file for details.

GitHub

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

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