Skip to content

Instantly share code, notes, and snippets.

@dipkasyap
Created June 16, 2016 10:35
Show Gist options
  • Save dipkasyap/d0457f965a3aca4a4f2b2ff307cd249a to your computer and use it in GitHub Desktop.
Save dipkasyap/d0457f965a3aca4a4f2b2ff307cd249a to your computer and use it in GitHub Desktop.
String extenssion and Reg-Ex
//
// StringExtension.swift
// EddyCab
//
// Created by Bikram on 12/24/15.
// Copyright © 2015 Esig. All rights reserved.
//
import Foundation
extension String {
mutating func addString(str: String) {
self = self + str
}
var appendKM:String{
return self + "Km"
}
var appendPlist:String{
return self + ".plist"
}
func giveShortDate()->String{
//"2015-12-31 11:04:06"
let formater = NSDateFormatter()
formater.dateFormat = "yyyy-MM-dd HH:mm:ss"
let tempDate = formater.dateFromString(self)
print(tempDate)
formater.dateStyle = .MediumStyle
return(formater.stringFromDate(tempDate!))
}
var validUserName: Bool {
do {
let regex = try NSRegularExpression(pattern: "[A-Z]+[0-9]?", options: .CaseInsensitive)
return regex.firstMatchInString(self, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0,self.characters.count)) != nil
} catch _ {
//print("got error on regex")
}
return false
}
var isValidEmail:Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(self)
}
var isValidName:Bool {
let nameRegEx = "[A-Za-z]{4,}+\\s+[A-Za-z]{0,}"
let nameTest = NSPredicate(format:"SELF MATCHES %@", nameRegEx)
return nameTest.evaluateWithObject(self)
}
//Validate Email
var isEmail: Bool {
do {
let regex = try NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .CaseInsensitive)
return regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) != nil
} catch {
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment