【问题标题】:regex to find hashtags in tweet not working correctly正则表达式在推文中查找主题标签无法正常工作
【发布时间】:2013-05-17 03:21:21
【问题描述】:

我正在尝试构建一个函数来在 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。所以推文可以包含以下内容。 &lt;a href='http://google.be' target='_blank'&gt;http://google.be&lt;/a&gt;

现在有时它会在其他链接周围放置另一个标签,而不仅仅是在主题标签周围。

有人可以帮我解决这个问题吗?

提示

我正在尝试将以下 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

【问题讨论】:

  • 请提供一个失败的测试用例的例子。我认为您的 ...google.be... 字符串没有问题。
  • 我已经编辑了我的问题 :)

标签: iphone ios objective-c regex nsregularexpression


【解决方案1】:

问题是您在循环匹配时正在修改您的 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2012-05-25
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多