正则表达式在tweet中找不到正确的标签

时间:2013-05-22 07:11:29

标签: iphone ios objective-c regex nsregularexpression

我正在尝试构建一个函数,以便在tweet中找到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

1 个答案:

答案 0 :(得分:1)

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

编辑:在此处添加解决方案:

{{1}}