Objective-C从字符串中获取电子邮件地址

时间:2013-02-15 22:43:48

标签: ios objective-c

我有一个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>

                      

2 个答案:

答案 0 :(得分:1)

使用NSRegularExpression的模式{/ 3}}来查看。

答案 1 :(得分: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);
}