Created
June 16, 2016 10:35
-
-
Save dipkasyap/d0457f965a3aca4a4f2b2ff307cd249a to your computer and use it in GitHub Desktop.
String extenssion and Reg-Ex
This file contains 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
// | |
// 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