【问题标题】:NSRegularExpression ISSUENSRegularExpression 问题
【发布时间】:2012-12-01 08:40:03
【问题描述】:

我正在使用 NSRegularExpression 来阅读文本并找出主题标签。 这是我在 regularExpressionWithPattern 中使用的 NSString。

- (NSString *)hashtagRegex
{
    return @"#((?:[A-Za-z0-9-_]*))";
    //return @"#{1}([A-Za-z0-9-_]{2,})";

}

这是我的方法:

 // Handle Twitter Hashtags
  detector = [NSRegularExpression regularExpressionWithPattern:[self hashtagRegex] options:0 error:&error];
  links = [detector matchesInString:theText options:0 range:NSMakeRange(0, theText.length)];
  current = [NSMutableArray arrayWithArray:links];

    NSString *hashtagURL =   @"http://twitter.com/search?q=%23";
  //hashtagURL = [hashtagURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

  for ( int i = 0; i < [links count]; i++ ) {

    NSTextCheckingResult *cr = [current objectAtIndex:i];

    NSString *url = [theText substringWithRange:cr.range];

    NSString *nohashURL = [url stringByReplacingOccurrencesOfString:@"#" withString:@""];
    nohashURL = [nohashURL stringByReplacingOccurrencesOfString:@" " withString:@""];

    [theText replaceOccurrencesOfString:url 
                             withString:[NSString stringWithFormat:@"<a href=\"%@%@\">%@</a>", hashtagURL, nohashURL, url]
                                options:NSLiteralSearch 
                                  range:NSMakeRange(0, theText.length)];
    current = [NSMutableArray arrayWithArray:[detector matchesInString:theText options:0 range:NSMakeRange(0, theText.length)]];

  }

  [theText replaceOccurrencesOfString:@"\n" withString:@"<br />" options:NSLiteralSearch range:NSMakeRange(0, theText.length)];

  [_aWebView loadHTMLString:[self embedHTMLWithFontName:[self fontName] 
                                                   size:[self fontSize] 
                                                   text:theText]
                    baseURL:nil];

一切正常,但当我使用这样的字符串时,它发现了一个小问题:

NSString * theText = @"#twitter #twitterapp #twittertag";

我的代码仅在每个单词上突出显示#twitter,而不是第二部分(#twitter #twitter(app) #twitter(tag))。 我希望有人能帮助我!

谢谢你:)

【问题讨论】:

    标签: iphone objective-c regex twitter hashtag


    【解决方案1】:

    声明

    [theText replaceOccurrencesOfString:url 
                             withString:[NSString stringWithFormat:@"<a href=\"%@%@\">%@</a>", hashtagURL, nohashURL, url]
                                options:NSLiteralSearch 
                                  range:NSMakeRange(0, theText.length)];
    

    正在用替换字符串替换字符串url所有 个实例。在您给出的示例中,第一次通过循环时,url@"#twitter",并且theText 中该字符串的所有三个出现都被一次性替换。这就是theText 的样子:

    <a href="http://twitter.com/search?q=%23twitter">#twitter</a> <a href="http://twitter.com/search?q=%23twitter">#twitter</a>app <a href="http://twitter.com/search?q=%23twitter">#twitter</a>tag
    

    所以,当然,接下来的两次循环,结果并不完全是你所期望的......!

    我认为解决方法是限制替换的范围:

    [theText replaceOccurrencesOfString:url 
                             withString:[NSString stringWithFormat:@"<a href=\"%@%@\">%@</a>", hashtagURL, nohashURL, url]
                                options:NSLiteralSearch 
                                  range:cr.range];
    

    【讨论】:

    • 你说得对!我完全心不在焉,我没有注意到......谢谢它就像一个魅力!
    猜你喜欢
    • 2011-11-11
    • 2015-01-11
    • 2015-07-30
    • 2012-08-19
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多