Just to make regular expression API usable in iOS/OS X development.
SwiftyRegex is a microlibrary (<100 LOC) which simply wraps clunky NSRegularExpression
class API.
It is opinionated and focuses on most common use cases dealing with regular expressions:
- to check if string matches to regex
- to find matches of specified regex in string
- to find capture groups
()
in string - regular expression must be valid, otherwise program will crash with runtime error
Everything should be doable in oneliner, as simple as the task is. Compare JavaScript built-in regular expression class:
var rex = RegExp("\\d+")
console.log(rex.test("123"));
// true
console.log(rex.exec("foo 123 abc"));
// [ '123', index: 4, input: 'foo 123 abc' ]
Wouldn't be cool to have something similar available in Mac/iOS programming?
Initialize instance of Regex class with regular expression. Runtime error will be thrown in case of bad syntax. This is intended as bad regexp is likely a programming error anyway.
let rex = Regex("\\d+")
Regex("\\d+").test("123") == true
I am for using custom/overloaded operators sparringly, but this one is borrowed from Perl world:
"12345" =~ "\\d+" == true
Returns array of strings matching regexp. If no matches were found, empty array is returned, following Null Object pattern.
Regex("\\d+").matches("123 456") == [ "123", "456" ]
Useful for parsing simple text documents. At index zero there is full match, and starting with index 1, each capture groups follow.
Regex("(\\d{2})-(\\d{3})").captures("31-234") == ["31-234", "31", "234"]
Just copy SwiftyRegex.swift to your project. Everything is in one file.
use_frameworks!
pod "SwiftyRegex", "1.0.0"
Because of limitations, SwiftyRegex will be included as framework, so minimum deployment target is iOS 8 and OS X 10.9.
SwiftyRegex has been written by Tomek Cejner by urgent need of comfortable use of regular expressions in his project. Forks, pull requests are always welcome.
Find me on Twitter as @tomekcejner
I also have a website