IOS Regular Expressions

Source: Internet
Author: User

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

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.