IOS Regular Expression detailed _ios

Source: Internet
Author: User

First, the system with the regular expression of the use of

In addition to being able to use, and nsscanner this class can achieve some of the same effect

1. Creating Regular Expression objects

 
 

2, the method that the regular expression object can call

/**1 * Regular Expression matching * * @param string Matching * @param options general fill in 0 * @param range match within this range * @param block within the block will be matched to the result of a return Nstextcheckingresult, the Range property of this object allows you to view the location and length of the matched string, and the substring method can get the matched string */-(void) Enumeratematchesinstring: (NSString *) string options: (nsmatchingoptions) options range: (nsrange) range Usingblock: ( void (^) (Nstextcheckingresult * __nullable result, nsmatchingflags flags, BOOL *stop)) blocks; /**2 * Regular Expression matching * * @param string Matching * @param options general fill in 0 * @param range matches within this range * @param block returns an array of each matching Nstex Tcheckingresult */-(Nsarray<nstextcheckingresult *> *) matchesinstring: (NSString *) string options: ( nsmatchingoptions) Options Range: (nsrange) range; /**3 * Regular Expression matching * * @param string Matching * @param options general fill in 0 * @param range matches within this range * @param block returns the number of matches to */-(nsuint Eger) numberofmatchesinstring: (NSString *) string options: (nsmatchingoptions) options range: (nsrange) range; /**4 * Regular Expression matching * * @param string Matching * @param options general fill in 0 * @param RANge match in this range * @param block returns the first Nstextcheckingresult object */-(Nullable Nstextcheckingresult *) Firstmatchinstring: ( NSString *) string options: (nsmatchingoptions) options range: (nsrange) range; /**5 * Regular Expression matching * * @param string Matching * @param options general fill in 0 * @param range matches within this range * @param block returns the first match to the Nstextcheck Ingresult object's range */-(Nsrange) rangeoffirstmatchinstring: (NSString *) string options: (nsmatchingoptions) options Range: (nsrange) range;3.nsstring//-(Nsrange) rangeofstring: (NSString *) searchstring options: ( nsstringcompareoptions) mask;//-(Nsrange) rangeofstring: (NSString *) searchstring options: (nsstringcompareoptions) Mask range: (nsrange) searchrange;//matching text-(void) matchin{nsstring *searchtext = @ "rangeof77str88ing";//Only match once Nsrange Range = [SearchText rangeofstring:@ "[0-9]+" options:nsregularexpressionsearch]; Get the matching result if (range.location!= nsnotfound) {NSString * searchstring= [SearchText Substringwithrange:range]; NSLog (@ "range.location =%lu range.length =%lu", (unsigned long) rAnge.location, (unsigned long) range.length); NSLog (@ "String =%@", searchstring); }else{NSLog (@ "no match");} 4, regular expression sample code nsstring *summarystring = [nsstring stringwithformat:@%@ problem%@ title | task Accuracy%@ | Job average accuracy%@ ", Questionright, Quest
Ionwrong, Questionrate, taskrate]; nsmutableattributedstring *summaryattributstring = [[Nsmutableattributedstring alloc] initwithstring:summarystring]
;
Regular matches, matching numbers and percent nsstring *reg = @ "[0-9]+|%";
Nserror *error = nil; Nsregularexpression *regex = [[Nsregularexpression alloc] initwithpattern:reg options:
Nsregularexpressioncaseinsensitive error:&error];
Nsarray *array = [Regex matchesinstring:summarystring options:0 range:nsmakerange (0, summarystring.length)];
if (Array.count) {for (int i = 0; i < Array.count; i++) {Nstextcheckingresult *result = array[i];
Nsrange range = Result.range; [Summaryattributstring addattribute:nsforegroundcolorattributename value:[uicolor colorWithHexString:@ "#e71419"]
Range:range]; }} Self.summaryLabel.attributedText = Summaryattributstring; 

5. Predicate

Verify existence-(BOOL) Validateregular: (NSString *) Regular Matchtext: (NSString *) text; { 
//Regular expression format 
//nsstring* regular=@ "^1[0-9]{6}$"; 
Create predicate 
nspredicate *numberpre = [nspredicate predicatewithformat:@ "SELF matches%@", regular]; 
Match string, if can match return yes, otherwise return no; 
BOOL ismatch = [Numberpre evaluatewithobject:text]; 
return IsMatch;}

The above is a small set to introduce the iOS regular expression, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.