In iOS development, regular is always the most commonly used, but also can not remember, now do some simple summary
We look for the regular online, to have ' \ ', this in iOS is an escape character, need ' \ \ ' This
intMainintargcConst Char*argv[]) {@autoreleasepool {//Insert code here ...//1. Create a regular expression//[]: Find one of the internal strings//"[A-z]": lowercase//"[a-za-z]": Case//"[a-za-z0-9]": Case and number//" \\d" equals "[0-9]"//" \\d{3}" is equivalent to "\\d\\d\\d", which means finding a number of 3 consecutive numbers//"\\d{2,4}" indicates that 2 or 4 digits match. such as "342455", get two results "3424" and "Three "// . Match any character other than line break//\w match letters or numbers or underscores or kanji//\s matches any whitespace character//\d Matching numbers//\b Match the beginning or end of a word//^ matches the start of the string. such as @ "^\\d{2}": Indicates starting with 2 digits, "345DD" matches 1, "1d2e33" does not match//$ matches the end of the string. such as @ "\\d{2}$": means ending with 2 digits, "ddd22" matches 1, "Dd3s3s" does not match//For example, a mobile phone: @ "^1[3578]\\d{9}$", which starts with 1, matches any 3,5,7,8 in the middle, and ends with 9 digits//Of course, the correct phone "1[3578]\\d{9}$" and "1[3578]\\d{9}" write can be matched. //* Repeat 0 or more times//+ Repeat one or more times// ? Repeat 0 or one timeNSString*username =@"13800138000"; NSString*pattern =@"^1[3578]\\d{9}$"; Nsregularexpression*regex = [[Nsregularexpression alloc] initwithpattern:pattern options:0Error:nil]; //Test StringNsarray *resultarray = [Regex matchesinstring:username options:0Range:nsmakerange (0, Username.length)]; NSLog (@"%zd", resultarray.count); } return 0;}
Print out matching characters
NSString *username =@"ddd333jda892dd34dsss"; NSString*pattern =@"[a-za-z]+";//matches one or more times and prints a continuous EnglishNsregularexpression *regex = [[Nsregularexpression alloc] initwithpattern:pattern options:0Error:nil]; Nsarray*resultarray = [Regex matchesinstring:username options:0Range:nsmakerange (0, Username.length)]; for(Nstextcheckingresult *resultinchresultarray) {NSLog (@"%@ %@", Nsstringfromrange (Result.range), [UserName SubstringWithRange:result.range]); }
Result (will print a sequential result):
IOS Regular Expressions