One, the system comes with regular expression usage
In addition to the regular can be used, there are Nsscanner this class can achieve some of the same effect
1. Create a regular Expression object
NSString * reg = @ "[0-9]+";//String Regular expression
Nserror *error = null;//Error Object
Nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:reg options: Nsregularexpressioncaseinsensitive error:&error];//Regular Expression object matching Reg
2. Methods that can be called by regular expression objects
/**1 * Regular Expression matching *
* @param string matches in this string
* @param options generally fill 0
* @param range matches within this range
* @param block will return the matched result to nstextcheckingresult one by one, and the Range property of this object can be used to view the location and length of the matched string. The same over 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)) block;
/**2 * Regular Expression matching *
* @param string matches in this string
* @param options generally fill 0
* @param range matches within this range
* @param block returns an array in which each matched Nstextcheckingresult
*/-(Nsarray<nstextcheckingresult *> *) matchesinstring: (NSString *) string options: (nsmatchingoptions) options Range: (nsrange) range;
/**3 * Regular Expression matching *
* @param string matches in this string
* @param options generally fill 0
* @param range matches within this range
* @param block returns the number of matches
*/-(Nsuinteger) numberofmatchesinstring: (NSString *) string options: (nsmatchingoptions) options range: (nsrange) Range ;
/**4 * Regular Expression matching *
* @param string matches in this string
* @param options generally fill 0
* @param range matches within this range
* @param block returns the first match to the Nstextcheckingresult object
*/-(Nullable Nstextcheckingresult *) firstmatchinstring: (NSString *) string options: (nsmatchingoptions) options Range :(nsrange) range;
/**5 * Regular Expression matching *
* @param string matches in this string
* @param options generally fill 0
* @param range matches within this range
* @param block returns the range of the Nstextcheckingresult object that was first matched to
*/-(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;
Match text
-(void) matchin{
NSString *searchtext = @ "rangeof77str88ing";
Match only once
Nsrange range = [SearchText rangeofstring:@ "[0-9]+" options:nsregularexpressionsearch];
Get the matching results.
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%@ Problem | Task Accuracy Rate%@ | Job average correct rate%@ ", Questionright, Questionwrong, Questionrate, taskrate];
nsmutableattributedstring *summaryattributstring = [[Nsmutableattributedstring alloc] initwithstring:summarystring] ;
Regular matching, matching numbers and percent semicolons
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. Predicates
Verify existence-(BOOL) Validateregular: (NSString *) Regular Matchtext: (NSString *) text;
{ //Regular expression format
nsstring* [email protected] "^1[0-9]{6}$";
Create predicate
Nspredicate *numberpre = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regular];
Matches the string, returns no if it matches the return yes;
BOOL IsMatch = [Numberpre evaluatewithobject:text];
return isMatch;
}
IOS Regular Expressions