Reputation: 2946
I have a text
var txt = "[email protected] heyyyyy cool [email protected]"
I want to extract the email address from the text and store it in an array. I want to do it with regular expression. I found the regular expression, but i am not able to save the email in to an array.
i tried
let regEx = "/(\\+[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)/gi"
if let email = NSPredicate(format: "SELF MATCHES %@", regEx) {
//what to do here
}
Or am i doing wrong? I know this is a basic question. Please help
Thanks in advance.
Upvotes: 2
Views: 3217
Reputation: 3621
These code worked for me. You can checkout email regex from here.
SWIFT 5
func extractEmailAddrIn(text: String) -> [String] {
var results = [String]()
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let nsText = text as NSString
do {
let regExp = try NSRegularExpression(pattern: emailRegex, options: .caseInsensitive)
let range = NSMakeRange(0, text.count)
let matches = regExp.matches(in: text, options: .reportProgress, range: range)
for match in matches {
let matchRange = match.range
results.append(nsText.substring(with: matchRange))
}
} catch (let error) {
print(error)
}
return results
}
SWIFT 3
func extractEmailAddrIn(text: String) -> [String] {
var results = [String]()
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let nsText = text as NSString
do {
let regExp = try NSRegularExpression(pattern: emailRegex, options: NSRegularExpressionOptions.CaseInsensitive)
let range = NSMakeRange(0, text.characters.count)
let matches = regExp.matchesInString(text, options: .ReportProgress, range: range)
for match in matches {
let matchRange = match.range
results.append(nsText.substringWithRange(matchRange))
}
} catch _ {
}
return results
}
Upvotes: 6
Reputation: 4668
Here is the String extensions I have created to extract emails from It works well in swift 4.
extension String {
func getEmails() -> [String] {
if let regex = try? NSRegularExpression(pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}", options: .caseInsensitive)
{
let string = self as NSString
return regex.matches(in: self, options: [], range: NSRange(location: 0, length: string.length)).map {
string.substring(with: $0.range).lowercased()
}
}
return []
}
}
Usage
let test = "Precision Construction John Smith cONSTRUCTION WORKER 123 Main St, Ste. 30o
www.precsioncontructien.com [email protected] [email protected] [email protected] 555.555.5SS5"
let emails = test.getEmails()
print(emails)
// results ["[email protected]", "[email protected]", "[email protected]"]
Upvotes: 0
Reputation: 71
Do you need the plus sign in the mail address?
Without + sign in the address:
([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)
Result: ["[email protected]", "[email protected]"]
With + sign in the address:
([\\+a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)
Result: ["[email protected]", "[email protected]"]
Upvotes: 0
Reputation: 16976
I recommend using the NSRegularExpression
class instead of NSPredicate
. The format for the regular expressions is from the ICU.
Here is one way to do it:
let pattern = "(\\+[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)"
let regexp = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive, error: nil)
let str = "[email protected] heyyyyy cool [email protected]" as NSString
var results = [String]()
regexp?.enumerateMatchesInString(str, options: NSMatchingOptions(0), range: NSRange(location: 0, length: str.length), usingBlock: { (result: NSTextCheckingResult!, _, _) in
results.append(str.substringWithRange(result.range))
})
// Gives [[email protected], [email protected]]
Upvotes: 3
Reputation: 38919
Looks like your regex name is wrong. You declare it as regEx
but in your NSPredicate
you use emailRegEx
.
Upvotes: 0