Call can throw, but it is not marked with 'try' and the error is not handled: NSRegularExpression
NSRegularExpression(pattern:)
throws an error if the pattern is invalid. In your case, the pattern is fixed, so an invalid patternwould be a programming error.
This is a use-case for the "forced-try" expression with try!
:
extension String { func isEmail() -> Bool { let regex = try! NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", options: [.CaseInsensitive]) return regex.firstMatchInString(self, options:[], range: NSMakeRange(0, utf16.count)) != nil }}
try!
disables the error propagation so that the method does notthrow an error (which the caller has to catch). It will abort with aruntime exception if the pattern is invalid, which helps to findprogramming errors early.
Note also that NSRange()
counts the length of NSString
, i.e. the number of UTF-16 code points, so characters.count
should be utf16.count
, otherwise it might crashe.g. with Emoji characters.
That's because that initializer can now throw an exception, so you need to try
to call it and be prepared to catch
the exception. You can do this by adding try
before the initializer and annotating your method with throws
.
extension String { func isEmail() throws -> Bool { let regex = try NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", options: [.CaseInsensitive]) return regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, characters.count)) != nil }}
Then when you want to call the method, do it from within a do
block and catch the error that comes out.
do { try "person@email.com".isEmail()} catch { print(error)}
Note: I've also updated your regex.firstMatchInString
call to reflect the fact that the options
parameter can no longer take a nil value.
If you don't like try catch :
extension String { func matchPattern(patStr:String)->Bool { var isMatch:Bool = false do { let regex = try NSRegularExpression(pattern: patStr, options: [.CaseInsensitive]) let result = regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, characters.count)) if (result != nil) { isMatch = true } } catch { isMatch = false } return isMatch }}
check string is correct email format :
let emailInput:String = "checkthis@gmail.com"if (emailInput.matchPattern("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$")){ print("this is e-mail!")}