Regex
![GitHub license](https://img.shields.io/badge/license-Apache 2.0-lightgrey.svg)
Advanced regular expressions for Swift
Goals
Regex library was mainly introduced to fulfill the needs of Swift Express - web application server side framework for Swift.
Still we hope it will be useful for everybody else.
Getting started
Installation
Package Manager
Add the following dependency to your Package.swift:
.Package(url: "https://github.com/crossroadlabs/Regex.git", majorVersion: 0)
Run swift build
and build your app. Package manager is supported on OS X, but it's still recommended to be used on Linux only.
CocoaPods
Add the following to your Podfile:
pod 'CrossroadRegex'
Make sure that you are integrating your dependencies using frameworks: add use_frameworks!
to your Podfile. Then run pod install
.
Carthage
Add the following to your Cartfile:
github "crossroadlabs/Regex"
Run carthage update
and follow the steps as described in Carthage's README.
Examples
Hello Regex:
All the lines below are identical and represent simple matching. All operators and matches
function return Bool
//operator way, can match either regex or string containing pattern
"l321321alala" =~ "(.+?)([1,2,3]*)(.*)".r
"l321321alala" =~ "(.+?)([1,2,3]*)(.*)"
//similar function
"(.+?)([1,2,3]*)(.*)".r!.matches("l321321alala")
Operator !~
returns true
if expression does NOT match:
"l321321alala" !~ "(.+?)([1,2,3]*)(.*)".r
"l321321alala" !~ "(.+?)([1,2,3]*)(.*)"
//both return false
Accessing groups:
// strings can be converted to regex in Scala style .r property of a string
let digits = "(.+?)([1,2,3]*)(.*)".r?.findFirst("l321321alala")?.group(2)
// digits is "321321" here
Named groups:
let regex:RegexType = try Regex(pattern:"(.+?)([1,2,3]*)(.*)",
groupNames:"letter", "digits", "rest")
let match = regex.findFirst("l321321alala")
if let match = match {
let letter = match.group("letter")
let digits = match.group("digits")
let rest = match.group("rest")
//do something with extracted data
}
Replace:
let replaced = "(.+?)([1,2,3]*)(.*)".r?.replaceAll("l321321alala", replacement: "$1-$2-$3")
//replaced is "l-321321-alala"
Replace with custom replacer function:
let replaced = "(.+?)([1,2,3]+)(.+?)".r?.replaceAll("l321321la321a") { match in
if match.group(1) == "l" {
return nil
} else {
return match.matched.uppercaseString
}
}
//replaced is "l321321lA321A"
Split:
In the following example, split() looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces from the string. nameList is the array returned as a result of split().
let names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand"
let nameList = names.split("\\s*;\\s*".r)
//name list contains ["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand"]
Split with groups:
If separator contains capturing parentheses, matched results are returned in the array.
let myString = "Hello 1 word. Sentence number 2."
let splits = myString.split("(\\d)".r)
//splits contains ["Hello ", "1", " word. Sentence number ", "2", "."]
Roadmap
- v1.0: stable release (once we will see that no issues are coming)
Changelog
- v0.5.1
- Minor linux build related fixes
- v0.5
- package manager support
- full linux support 🐧
- v0.4.1
- support for optionally present groups
- v0.4
- iOS, tvOS and watchOS support
- Pod supports watchOS
- automated pod deployment
- v0.3
- Split
- Matches
- CocoaPod
- Syntactic sugar operators (
=~
and!~
)
- v0.2
- Replace functions
- Carthage support
- v0.1
- basic find functions for OS X and iOS
Contributing
To get started, sign the Contributor License Agreement.