Swiftpack.co -  getGuaka/Regex as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
getGuaka/Regex
Cross-platform Swift regex library
.package(url: "https://github.com/getGuaka/Regex.git", from: "0.2.0")

Regex

Build Status codecov Platform Language: Swift Carthage

Why?

If you are developing cross platform (macOS and Linux) command line apps, and you want to use Regular expressions, Regex is just what you need.

You can use Regex with Guaka to create aweseome command line applications.

Usage

  • Checking if a string matches a pattern.
let r = try! Regex(pattern: "Hello [a-z]+ame")
r.matches("Hello Name")
  • Capture a string with a regex capture group
let r = try! Regex(pattern: "Hello (.*) name")
let result = r.captures(string: "Hello mr name")

result is an array of CaptureResult

  • Using the ~= operator
// Regex ~= String
let value = try! Regex(pattern: "Hello [a-z]+ame") ~= "Hello Name"
// value is true

// String ~= Regex
let value = "Hello Name" ~= try! Regex(pattern: "Hello [a-z]+ame")
// value is true
  • Using regex matching with switch
switch "Hello I am on swift" {
  case try! Regex(pattern: "Hello [a-z] am .*"):
    // First
  case try! Regex(pattern: ".*"):
    // Second
}

The first passing regex will be matched. In the example above, the first case is matched.

String extensions

Replace a pattern with a string:

"This string is wrong".replacing(pattern: "w.*g", withString: "right")
// "This string is right"

CaptureResult

CaptureResult represent a captured string, it contains:

  • originalString the original string
  • startIndex the capture start index
  • endIndex the capture end index
  • range the range of the captured string
  • string the captured string

RegexOptions

RegexOptions optionset can be passed when initilaizing a Regex object. For a discussion on the meaning of these flags, refer to GNU regex documentation

MatchOptions

matches(_:options:) accepts the MatchOptions option set. For a discussion on the meaning of these flags, refer to GNU regex documentation

Installation

You can install Regex using Swift package manager (SPM) and carthage

Swift Package Manager

Add Regex as dependency in your Package.swift

import PackageDescription

let package = Package(name: "YourPackage",
  dependencies: [
    .Package(url: "https://github.com/getGuaka/Regex.git", majorVersion: 0),
  ]
)

Carthage

github "getGuaka/Regex"

Tests

Tests can be found here.

Run them with

swift test

Contributing

Just send a PR! We don't bite ;)

GitHub

link
Stars: 16
Last commit: 2 years ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

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