IOS Regular Expressions

Source: Internet
Author: User

1. Using regular expressions in iOS, use the Nsregularexpression class

Instantiation method:

Nsregularexpression *regex = [[nsregularexpression alloc]initwithpattern:@ write regular expression in quotation marks ' options: Nsregularexpressioncaseinsensitive Error:nil];

Nsregularexpressioncaseinsensitive refers to case sensitive

1.1 Verifying the phone number with regular expressions

Example code:

1         //Suppose the phone number starts with 13\15\18 only .2NSString *str =@"13510188591";3         4         //instantiating a regular expression object5Nsregularexpression *regex = [[Nsregularexpression alloc]initwithpattern:@"1[358][0-9]{9}"options:nsregularexpressioncaseinsensitive Error:nil];6         //Match7Nsarray *matches = [Regex matchesinstring:str options:nsmatchingreportcompletion Range:nsmakerange (0, Str.length)];8         9         //Judgment,Ten         if(matches.count) { OneNSLog (@"match it to the"); A             //Regular expression on the number of bits do not control precisely, if the 100-bit number is provided, the above code will be considered to match, so you need to use the following code to improve the phone number matching -             if(Str.length = = One) { -NSLog (@"and the phone number!"); the             } -              -}Else{ -NSLog (@"There's no match ."); +}
1.2 Other common methods

-(Nstextcheckingresult *) firstmatchinstring: (NSString *) string options: (nsmatchingoptions) options range: (Nsrange) Range

-(Nsuinteger) numberofmatchesinstring: (NSString *) string options: (nsmatchingoptions) options range: (nsrange) range;

2. Useful 3rd party Framework Regexkitlite

The library relies on libicucore.dylib.

If it's an arc project, you have to add-fno-objc-arc.

Example code:

1         //Suppose the phone number starts with 13\15\18 only .2NSString *str =@"135101885910000";3NSString *regex =@"1[358][0-9]{9}";4[Str enumeratestringsmatchedbyregex:regex usingblock:^ (Nsinteger capturecount, NSString *Const__unsafe_unretained *capturedstrings,ConstNsrange *capturedranges,volatileBOOL *Conststop) {5NSLog (@"number of matches:%ld", capturecount);6NSLog (@"string matched to:%@",*capturedstrings);7NSLog (@"match to the range:%@", Nsstringfromrange (*capturedranges));8             9}];

Another common usage, printing a split string

1         //Suppose the phone number starts with 13\15\18 only .2NSString *str =@"13512345678abcd18912345678-15812345678";3NSString *regex =@"1[358][0-9]{9}";4 5[Str enumeratestringsseparatedbyregex:regex usingblock:^ (Nsinteger capturecount, NSString *Const__unsafe_unretained *capturedstrings,ConstNsrange *capturedranges,volatileBOOL *Conststop) {6NSLog (@"number of matches:%ld", capturecount);7NSLog (@"string matched to:%@",*capturedstrings);8NSLog (@"-----Match to the range:%@", Nsstringfromrange (*capturedranges));9}];

Output Result:

Number of matches: 1

The string to match:

-----Match to the range:{0, 0}

Number of matches: 1

Match to the string: ABCD

-----Match to the range:{11, 4}

Number of matches: 1

String to match:-

-----Match to the range:{26, 1}

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.