我正在尝试建立一个函数来查找tweest中的hashtags。并用HTML <a> tag包围它们。这样我就可以链接到他们。这是我的工作。

NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:\\s|\\A)[##]+([A-Za-z0-9-_]+)" options:0 error:&error];
NSArray* matches = [regex matchesInString:tweetText options:0 range:NSMakeRange(0, [tweetText length])];
for ( NSTextCheckingResult* match in matches )
{
    NSString* matchText = [tweetText substringWithRange:[match range]];
    NSString *matchText2 = [matchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *search = [matchText2 stringByReplacingOccurrencesOfString:@"#"
                                                            withString:@""];
    NSString *searchHTML= [NSString stringWithFormat:@"<a href='https://twitter.com/search?q=%%23%@'>%@</a>",search,matchText];
    tweetText = [tweetText stringByReplacingOccurrencesOfString:matchText
                                                    withString:searchHTML];
    NSLog(@"match: %@", tweetText);
}

在执行此功能之前,将tweetText循环通过另一个功能以找到URL。因此该推文可以包含以下内容。 <a href='http://google.be' target='_blank'>http://google.be</a>
现在有时将另一个标签放置在其他链接周围,而不仅是井号标签周围。

有人可以帮我这个忙吗?

提示

我正在尝试将以下JAVA代码转换为OBJ-C
  String patternStr = "(?:\\s|\\A)[##]+([A-Za-z0-9-_]+)"
     Pattern pattern = Pattern.compile(patternStr)
     Matcher matcher = pattern.matcher(tweetText)
     String result = "";

     // Search for Hashtags
     while (matcher.find()) {
     result = matcher.group();
     result = result.replace(" ", "");
     String search = result.replace("#", "");
     String searchHTML="<a href='http://search.twitter.com/search?q=" + search + "'>" + result + "</a>"
     tweetText = tweetText.replace(result,searchHTML);
     }

编辑
Gers, we kijken er al naar uit! “@GersPardoel: We zitten in België straks naar Genk!!<a href='<a href<a href='https://twitter.com/search?q=%23='http'>='http</a>s://twitter.com/search?q=%23https:/'>https:/</a>/twitter.com/search?q=%23engaan'> #engaan</a>” #GOS12 #genk #fb

最佳答案

问题是您在遍历匹配项时正在修改tweetText变量(tweetText = ...)。想象下一次代码进入循环时会发生什么?由于substringWithRange是在原始字符串上创建的,因此无法正常工作。尝试纠正问题,如果无法解决,请在此处检查解决方案:http://pastebin.com/DyQqtRzA

编辑:在这里添加解决方案:

NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:\\s|\\A)[##]+([A-Za-z0-9-_]+)" options:0 error:&error];
NSArray* matches = [regex matchesInString:tweetText options:0 range:NSMakeRange(0, [tweetText length])];
NSString* processedString = [[tweetText copy] autorelease];
for ( NSTextCheckingResult* match in matches )
{
    NSString* matchText = [tweetText substringWithRange:[match range]];
    NSString *matchText2 = [matchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *search = [matchText2 stringByReplacingOccurrencesOfString:@"#"
                                                            withString:@""];
    NSString *searchHTML= [NSString stringWithFormat:@"<a href='https://twitter.com/search?q=%%23%@'>%@</a>",search,matchText];
    processedString = [processedString stringByReplacingOccurrencesOfString:matchText
                                                                 withString:searchHTML];
    NSLog(@"match: %@", processedString);
}

09-27 04:31