Created
March 8, 2016 03:45
-
-
Save Thunderbird7/c51a4fb35cfd60428127 to your computer and use it in GitHub Desktop.
Regular Expression helper in Swift 2 / match, replace with your pattern!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Regex.swift | |
// Jitta | |
// | |
// Created by Yuttana Kungwon on 3/6/2559 BE. | |
// Copyright © 2559 Jitta Company. All rights reserved. | |
// | |
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(rawValue: 0) | |
matchingOptions = NSMatchingOptions(rawValue: 0) | |
updateRegex() | |
} | |
mutating func updateRegex() { | |
do { | |
regex = try NSRegularExpression(pattern: pattern, options: expressionOptions) | |
} catch { | |
regex = nil | |
} | |
} | |
} | |
extension String { | |
func matchRegex(pattern: Regex) -> Bool { | |
let range: NSRange = NSMakeRange(0, self.characters.count) | |
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, self.characters.count) | |
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 | |
"+66-887-922-3921".replace("[-\\s\\(\\)]", template: "") | |
*/ | |
// private function | |
// password only alphanumeric and/or symbols | |
func isPassword() -> Bool { | |
let pattern = "^[A-Za-z0-9!#$%&'()*+,-./:;<=>?@\\^_`{|}~]+$" | |
return self.match(pattern) | |
} | |
// email pattern | |
func isEmail() -> Bool { | |
let pattern = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" | |
return self.match(pattern) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment