Skip to content

Instantly share code, notes, and snippets.

@ningsuhen
Last active April 27, 2019 18:39
Show Gist options
  • Save ningsuhen/dc6e589be7f5a41e7794 to your computer and use it in GitHub Desktop.
Save ningsuhen/dc6e589be7f5a41e7794 to your computer and use it in GitHub Desktop.
Swift extension for Native String class to support Regex match and Regex replace. Credit - http://www.swift-studies.com/blog/2014/6/12/regex-matching-and-template-replacement-operators-in-swift
import Foundation
struct Regex {
var pattern: String {
didSet {
updateRegex()
}
}
var expressionOptions: NSRegularExpressionOptions {
didSet {
updateRegex()
}
}
var matchingOptions: NSMatchingOptions
var regex: NSRegularExpression?
init(pattern: String, expressionOptions: NSRegularExpressionOptions, matchingOptions: NSMatchingOptions) {
self.pattern = pattern
self.expressionOptions = expressionOptions
self.matchingOptions = matchingOptions
updateRegex()
}
init(pattern: String) {
self.pattern = pattern
expressionOptions = NSRegularExpressionOptions(0)
matchingOptions = NSMatchingOptions(0)
updateRegex()
}
mutating func updateRegex() {
regex = NSRegularExpression(pattern: pattern, options: expressionOptions, error: nil)
}
}
extension String {
func matchRegex(pattern: Regex) -> Bool {
let range: NSRange = NSMakeRange(0, countElements(self))
if pattern.regex != nil {
let matches: [AnyObject] = pattern.regex!.matchesInString(self, options: pattern.matchingOptions, range: range)
return matches.count > 0
}
return false
}
func match(patternString: String) -> Bool {
return self.matchRegex(Regex(pattern: patternString))
}
func replaceRegex(pattern: Regex, template: String) -> String {
if self.matchRegex(pattern) {
let range: NSRange = NSMakeRange(0, countElements(self))
if pattern.regex != nil {
return pattern.regex!.stringByReplacingMatchesInString(self, options: pattern.matchingOptions, range: range, withTemplate: template)
}
}
return self
}
func replace(pattern: String, template: String) -> String {
return self.replaceRegex(Regex(pattern: pattern), template: template)
}
}
/*
//e.g. replaces symbols +, -, space, ( & ) from phone numbers
"+91-999-929-5395".replace("[-\\s\\(\\)]", template: "")
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment