我有一个NSString包含几个电子邮件地址和一些其他随机文本。我希望能够只提取电子邮件地址。他们都有相同的域名,例如@ business.com。最好的办法是什么?
示例字符串(HTML)
<td colspan="2" bgcolor="#000000">
<p align="center"><b><font color="#FFFFFF" size="2">title</font></b></p></td>
</tr>
<tr>
<td valign="top">
<font size="2"><b>lastname</b>, firstname A., name@business.com</font><br>
<font size="2"><b>lastname</b>, firstname A., name@business.com</font><br>
<font size="2"><b>lastname</b>, firstname, name@business.com</font><br>
<font size="2"><b>lastname</b>, firstname, name@business.com</font><br>
答案 0 :(得分:1)
使用NSRegularExpression
的模式{/ 3}}来查看。
答案 1 :(得分:1)
我冒昧地为你创造了一个例子,因为我对自己感兴趣:
NSString *someHTML = @"<b>lastname</b>, firstname, test1@business.com</font><b>lastname</b>, firstname, test2@business.com</font><b>lastname</b>, firstname, test3@business.com</font>";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\w*@business.com)" options: NSRegularExpressionUseUnixLineSeparators error:nil];
NSArray *matches = [regex matchesInString:someHTML options:NSMatchingWithTransparentBounds range:NSMakeRange(0, someHTML.length)];
for (NSTextCheckingResult *result in matches)
{
NSString *email = [someHTML substringWithRange:[result range]];
NSLog(email);
}