正则表达式的使用 选取有用数据

 //话题  ##

    NSMutableAttributedString * contentStr = [[NSMutableAttributedString alloc] initWithString:Content];

    NSError *error = NULL;

    NSRegularExpression *topicRegex = [NSRegularExpression regularExpressionWithPattern:@"#[^#]+#" options:NSRegularExpressionCaseInsensitive error:&error];

//找到第一个符合条件的数据

    NSTextCheckingResult *result = [topicRegex firstMatchInString:bm.Content options:0 range:NSMakeRange(0, [bm.Content length])];

    if(result)

    {

        [contentStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:result.range];

        [contentStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]  range:result.range];

        [contentStr endEditing];

    }

     //链接

    NSRegularExpression *linkRegex = [NSRegularExpression regularExpressionWithPattern:@"链接的正则表达式" options:NSRegularExpressionCaseInsensitive error:&error];

//找到多个符合条件的数据

    NSArray *linkArr = [linkRegex matchesInString:bm.Content options:0 range:NSMakeRange(0, [bm.Content length])];

    NSMutableAttributedString * contentStr = [[NSMutableAttributedString alloc] initWithString:Content];

    if ([linkArr count] != 0) {

        for (NSTextCheckingResult *linkResult in linkArr) {

            NSLog(@"%@ ", [Content substringWithRange:result.range]);

        }

    }

原文地址:https://www.cnblogs.com/qiutangfengmian/p/4962226.html