首页 > 代码库 > iOS之— NSRegularExpression正则分割字符串

iOS之— NSRegularExpression正则分割字符串

社交化越来为社会所接受,在做社会化产品时,在回复或者转发时会@他人 展示出来,点击@user 跳转到该用户的介绍页面,这里首先要制定恰当的标签,比如:<at href=http://www.mamicode.com/"jecky://profile/239/">iJecky,然后客户端拿到文本时,要用到NSRegularExpression分割string,下面以富文本OHAttributedLabel来演示:

说明:本文中用到的contentLabel为OHAttributedLabel类型

示例:

NSString  *atstring = @"测试啊测试啊 <at href=http://www.mamicode.com/"jecky://profile/239/">iJecky 测试啊测试啊";


首先分割:

    NSMutableString *resultStr = [NSMutableStringstringWithCapacity:0];

    NSString *pattern =@"<at href=http://www.mamicode.com/"(.*?)\">(.*?)";

    NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:patternoptions:0error:nil];

    NSArray *matches = [regexmatchesInString:atstringoptions:0range:NSMakeRange(0,atstring.length)];

    NSUInteger lastIdx =0;

    for (NSTextCheckingResult* matchin matches)

    {

        NSRange range = match.range;

        if (range.location > lastIdx)

        {

            NSString  *temp = [atstringsubstringWithRange:NSMakeRange(lastIdx, range.location - lastIdx)];

            [resultStr appendString:temp];

        }

        NSString *link = [atstringsubstringWithRange:[match rangeAtIndex:1]];

        NSString *text = [atstring substringWithRange:[matchrangeAtIndex:2]];

        NSString *atName = [NSStringstringWithFormat:@"@%@",text];

        [resultStr appendString:atName];

        [markDic setObject:linkforKey:atName];

        lastIdx = range.location + range.length;

    }

    

    if (lastIdx <atstring.length)

    {

        NSString  *temp = [atstringsubstringFromIndex:lastIdx];

        [resultStr appendString:temp];

    }

    

    self.contentLabel.text = resultStr;


然后展示到contentLabel上

    NSRegularExpression* userRegex = [NSRegularExpressionregularExpressionWithPattern:@"\\B@\\w+"options:0error:nil];

    NSMutableAttributedString* mas = [self.contentLabel.attributedTextmutableCopy];

    

    [userRegex enumerateMatchesInString:self.contentLabel.textoptions:0

                                  range:NSMakeRange(0,self.contentLabel.text.length)

                             usingBlock:^(NSTextCheckingResult *match,NSMatchingFlags flags, BOOL *stop){

                                 NSString  *auser = [self.contentLabel.textsubstringWithRange:match.range];       

                                 NSString  *atlink = [markDicobjectForKey:auser];

                                 if (atlink) {

                                     [mas setLink:[NSURLURLWithString:[markDic objectForKey:auser]]range:match.range];// add it

                                 }

                             }];

    

    OHParagraphStyle* para = [OHParagraphStyledefaultParagraphStyle];

    para.firstLineHeadIndent =0;

    para.headIndent =5;

    para.tailIndent = -5;

    para.textAlignment =kCTTextAlignmentLeft;

    [mas setParagraphStyle:para];

    [OHASBasicMarkupParserprocessMarkupInAttributedString:mas];


    self.contentLabel.attributedText = mas;





iOS之— NSRegularExpression正则分割字符串