1

我有一个 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.,&nbsp;name@business.com</font><br>

<font size="2"><b>lastname</b>, firstname A.,&nbsp;name@business.com</font><br>

<font size="2"><b>lastname</b>, firstname,&nbsp;name@business.com</font><br>

<font size="2"><b>lastname</b>, firstname,&nbsp;name@business.com</font><br>

4

2 回答 2

1

看看使用查找电子邮件地址NSRegularExpression的模式。

于 2013-02-15T22:47:43.410 回答
1

由于我自己感兴趣,所以我冒昧地为您创建了一个示例:

NSString *someHTML = @"<b>lastname</b>, firstname,&nbsp;test1@business.com</font><b>lastname</b>, firstname,&nbsp;test2@business.com</font><b>lastname</b>, firstname,&nbsp;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);
}
于 2013-02-15T23:18:14.790 回答