SwiftRegExp 2.0

SwiftRegExp 2.0

TestsTested
LangLanguage SwiftSwift
License BSD
ReleasedLast Release Oct 2016
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Guillaume Laurent.



  • By
  • Guillaume Laurent

What is this

SwiftyRegExp is a simple and convenient Swift wrapper around NSRegularExpression. It was inspired by Ben Scheirman’s “RegEx in Swift” post

NOTE : a better framework with more features can be found at https://github.com/sharplet/Regex

Examples

Simple boolean match

let regexp = try RegExp("abc.*")
if regexp.isMatching("abcdef") {
  println("match!")
} else {
  println("error")
}

Get matched string

let regexp = try RegExp("abc.*")
if let match = "abcdef" =~ regexp {
   println("match : \(match)!")
} else {
   println("error")
}

Get all matches

Also works with capture groups :

let regexp = try RegExp("abc(.*)def(.*)")
let matches = regexp.allMatches("abcXXXdefYYYY")
for match in matches {
    println("match \(match)")
}

this will print “abcXXXdefYYYY”, “XXX”, “YYYY”

How to use

Simply drop the RegExp.swift file in your project