iOS-nsregularexpression Regular split string

Source: Internet
Author: User

Social more to be accepted by the community, in the social products, in the reply or forwarding will @ others display, click @user Jump to the user's introduction page, here first to develop appropriate tags, such as: <at href=\ "Jecky://profile/239\" >ijecky</at>, and then when the client gets the text, it uses Nsregularexpression to split the string, which is illustrated below with a rich text Ohattributedlabel:

Description: The Contentlabel used in this article is the Ohattributedlabel type

Example:

nsstring *atstring = @ " test it, test it. <at href=\ "jecky://profile/239\" >iJecky</at> test it, test it. ";


First split:

nsmutablestring *resultstr = [nsmutablestringstringwithcapacity :0];

nsstring *pattern =@ "<at href=\" (. *?) \ "> (. *?) </at> ";

    nsregularexpression *regex = [ nsregularexpression Regularexpressionwithpattern :p attern options : 0 error : nil

nsarray *matches = [regexmatchesinstring:atstring Options:0range:nsmakerange (0,atstring. length )];

nsuinteger lastidx =0;

for (nstextcheckingresult* Match in 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 :[match rangeatindex : 2 ];

nsstring *atname = [nsstringstringwithformat:@ "@%@", text];

[resultstr appendString: atname];

[markdic setobject: Linkforkey: atname];

lastidx = range. Location + range. length ;

}

if (Lastidx <atstring. length )

{

nsstring *temp = [atstringsubstringfromind Ex: Lastidx];

[resultstr appendString: temp];

}

self. Contentlabel . text = ResultStr;


and show it to Contentlabel.

nsregularexpression* Userregex = [nsregularexpression Regularexpressionwithpattern:@ "\\[email protected]\\w+"options: 0error:nil];

nsmutableattributedstring* mas = [self. Contentlabel . Attributedtext mutablecopy];

[Userregex enumeratematchesinstring:self. Contentlabel . text Options:0

Range:nsmakerange(0,s Elf. Contentlabel . text . length )

usingblock: ^ (nstextcheckingresult *match,nsmatchingflags flags, BOOL *stop) {

nsstring *auser = [self. Contentlabel . text substringwithrange: 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:p ARA];

[ohasbasicmarkupparserprocessmarkupinattributedstring: mas];


self. Contentlabel . Attributedtext = Mas;





iOS-nsregularexpression Regular split string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.