Latest 2.1
Homepage https://github.com/lostatseajoshua/SwiftExpression
License MIT
Platforms ios 8.0, requires ARC
Authors

Build Status Carthage compatible

Swift Expression is a Regex framework built with Swift to make it easier to work with NSRegularExpression. The framework provides low overhead to work with pattern matching.

Motivation

There is not a native Regex implemenation in Swift or Objective-C as there is in other languages such as javascript or ruby. This framework’s goal is to make it easier to work with Regex in a "Swifty" way. The framework is built on NSRegularExpression with just easy to call methods and easy objects to work with. To feel as native as possible the Swift String type has been extended with new methods for Regex.

Requirements

Xcode 8

Swift 3

iOS 8.0+

Code Example

Create a Regex object

A Regex can be created multiple ways.

Failable Initializer
if let regex = Regex("^\d") {
  // use regex object
}
Prefix operator

The prefix operator is used as a way to initialize a regex struct and is built on the failable initalizer.

if let regex = <>"^\d" {
  // use regex object
}
Infix operator

Use the custom infix to check for a match.

let str = "333-aa-bb"
let regexPatternStr = "^\d"

// string to search in on left hand side, regex pattern in string format on right hand side
if str ~= regexPatternStr {
  // a match is in the string continue processing
} else {
  // failed to find a single match in string
}
String extension methods

Get matches in string.

let match = str.match(regex) // Returns a Regex.Match struct
match.components // Returns a collection of tuples that are the substring match and range of the match [(String, Range<String.Index>)]
match.subStrings() // Returns a collection of the substring matches [String]
match.ranges() // Returns a collection of ranges of the matches [Range<String.Index>]

Replace

str.replace(regex, with: replaceStr) // Returns a new string with matches replaced with replacement string

Search

str.search(regex) // Returns an optional Int of the location of the range of the first match found in the string

Find

str.find(regex) // Returns true if a match exists in the string

Installation

@available(iOS 8, *)

CocoaPods

You can use Cocoapods to install SwiftExpression by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
  pod 'SwiftExpression'
end

Carthage

You can use Carthage to install SwiftExpression by adding it to your Cartfile:

github "lostatseajoshua/SwiftExpression"

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourProject",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/lostatseajoshua/SwiftExpression.git",
                 majorVersion: 2),
    ]
)

Manually

To use this library in your project manually do the following.

Add the SwiftExpression.framework to your project by downloading the framework from the releases

Demo

Checkout this demo project that was built with SwiftExpression. It highlights text in a UITextView using regex pattern that is inputted into a UITextField!

Demo Github project

Contributors

Joshua Alvarado – TwitterWebsite

License

This project is released under the MIT license.

Latest podspec

{
    "name": "SwiftExpression",
    "version": "2.1",
    "license": {
        "type": "MIT"
    },
    "homepage": "https://github.com/lostatseajoshua/SwiftExpression",
    "authors": {
        "Joshua Alvarado": "[email protected]"
    },
    "summary": "Swift library for working with Regex.",
    "source": {
        "git": "https://github.com/lostatseajoshua/SwiftExpression.git",
        "tag": "v2.1"
    },
    "source_files": "SwiftExpression/SwiftExpression/Regex.swift",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This