【发布时间】:2011-05-05 20:21:11
【问题描述】:
我目前正在使用 UIWebView 对来自 twitter 的帖子进行样式化。一些推文当然包含 URL,但不包含 <a> 标签。我可以提取 URL,但是我不确定如何添加 <a> 标签并将其放回推文中。然后,我将在这里使用相同的方法来添加指向@usernames 和#hashtags 的链接。这是我当前代码的示例:
NSString *tweet = @"Sync your files to your Google Docs with a folder on your desktop. Like Dropbox. Good choice, Google storage is cheap. http://ow.ly/4OaOo";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
NSString *match = [tweet substringWithRange:[expression rangeOfFirstMatchInString:tweet options:NSMatchingCompleted range:NSMakeRange(0, [tweet length])]];
NSLog(@"%@", match);// == http://ow.ly/4OaOo
最终,我希望最终的字符串看起来像这样:
Sync your files to your Google Docs with a folder on your desktop. Like Dropbox. Good choice, Google storage is cheap. <a href="http://ow.ly/4OaOo>http://ow.ly/4OaOo</a>
任何帮助将不胜感激。
【问题讨论】:
标签: iphone objective-c regex ios