Skip to content

Instantly share code, notes, and snippets.

@JackieQi
Forked from ningsuhen/Regex.swift
Last active August 31, 2017 21:46
Show Gist options
  • Save JackieQi/9bc36671468b93e1b8d084523af5e19f to your computer and use it in GitHub Desktop.
Save JackieQi/9bc36671468b93e1b8d084523af5e19f 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))
struct Regex {
var pattern: String {
didSet {
updateRegex()
}
}
var expressionOptions: NSRegularExpression.Options {
didSet {
updateRegex()
}
}
var matchingOptions: NSRegularExpression.MatchingOptions
var regex: NSRegularExpression?
init(pattern: String, expressionOptions: NSRegularExpression.Options, matchingOptions: NSRegularExpression.MatchingOptions) {
self.pattern = pattern
self.expressionOptions = expressionOptions
self.matchingOptions = matchingOptions
updateRegex()
}
init(pattern: String) {
self.pattern = pattern
expressionOptions = NSRegularExpression.Options.init(rawValue: 0)
matchingOptions = NSRegularExpression.MatchingOptions.init(rawValue: 0)
updateRegex()
}
mutating func updateRegex() {
regex = try? NSRegularExpression(pattern: pattern, options: expressionOptions)
}
}
extension String {
func matchRegex(pattern: Regex) -> Bool {
let range: NSRange = NSMakeRange(0, self.characters.count)
if pattern.regex != nil {
let matches: [AnyObject] = pattern.regex!.matches(in: self, options: pattern.matchingOptions, range: range)
return matches.count > 0
}
return false
}
func match(patternString: String) -> Bool {
return self.matchRegex(pattern: Regex(pattern: patternString))
}
func replaceRegex(pattern: Regex, template: String) -> String {
if self.matchRegex(pattern: pattern) {
let range: NSRange = NSMakeRange(0, self.characters.count)
if pattern.regex != nil {
return pattern.regex!.stringByReplacingMatches(in: self, options: pattern.matchingOptions, range: range, withTemplate: template)
}
}
return self
}
func replace(pattern: String, template: String) -> String {
return self.replaceRegex(pattern: 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