Swift regular expressions
/Another little something to copy and paste into a Swift Playground... Regular expression matching in Swift with a couple of little helper functions. Obviously the mapping to Objective C is key here. Otherwise it's pretty much business as usual.
import Foundation func matches(searchString:String, pattern : String)->Bool{ var error:NSError? let regex = NSRegularExpression .regularExpressionWithPattern(pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: &error) if (!error){ let matchCount = regex.numberOfMatchesInString(searchString, options: NSMatchingOptions.fromMask(0), range: NSMakeRange(0, countElements(searchString))) return matchCount > 0 } return false } func replace(searchString:String, pattern : String, replacementPattern:String)->String?{ var error:NSError? let regex = NSRegularExpression .regularExpressionWithPattern(pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: &error) if (!error){ let replacedString = regex.stringByReplacingMatchesInString(searchString, options: NSMatchingOptions.fromMask(0), range: NSMakeRange(0, countElements(searchString)), withTemplate: replacementPattern) return replacedString } return nil } matches("Hello, Swift", "Swift") println(replace("Hello Objective-C","Objective-C","Swift, Goodbye $0"))