IOS 类似微博,#话题#,@人,[表情] 网址 正则匹配

/**
 *获取需要处理的子字符串和子串的range
 */
-(NSArray<NSTextCheckingResult *> *)getBBSLetterSubStrRangeArrWithStr:(NSString *)str{
    //[...]表情
    //@"\\[[a-zA-Z\\u4e00-\\u9fa5]+\\]"
    NSString *emopattern=@"\\[[\u4e00-\u9fa5\\w]+\\]" ;
    //#...#话题
    //@"#[0-9a-zA-Z\\u4e00-\\u9fa5]+#"
    //   @"#[\u4e00-\u9fa5\\w\\s]+#";
    NSString *toppattern =  @"#[^#]+#";
    //@...@
    //@"@[0-9a-zA-Z\\U4e00-\\u9fa5]+"
    NSString *atpattern = @"@[\u4e00-\u9fa5\\w]+";
    //url
    //@"http://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"
    NSString *urlpattern = @"\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))";
    NSString *pattern = [NSString stringWithFormat:@"%@|%@|%@|%@",emopattern,toppattern,atpattern,urlpattern];
    NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray *results = [regular matchesInString:str options:0 range:NSMakeRange(0, str.length)];
    
    return results;
}

 

posted @ 2016-05-20 10:19  熹微_念  阅读(1029)  评论(0编辑  收藏  举报