在IOS應用中,經常要輸入數據,然輸入數據,就要校驗數據的合法性,這是我們很自然的聯想到web應用中的正則表達式。然而Cocoa並未支持正則表達式。這是我們可以使用第三方工具(RegexKitLite)來實現正則表達式。
1、下載(RegexKitLite)類庫,備用下載地址:RegexKitLite,將RegexKitLite.h/ RegexKitLite.m兩個文件添加到您的項目中;
2、將libicucore.dylib類庫導入;
3.將#import "RegexKitLite.h"加入
4.代碼如下就ok,驗證手機號是如下的string
NSString *regEx =@"\\b(1)[358][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\\b";
[loginPrompt.plainTextField.textisMatchedByRegex:regEx];如果匹配就返回yes;否則就返回no
5.如果要驗證另一種格式的字符串,譬如說郵箱。
NSString *email = @"[email protected]";
NSString *regex = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
if ([email isMatchedByRegex:regex])
{
NSLog(@"通過校驗!");
}
else
{
NSLog(@"未通過校驗,數據格式有誤,請檢查!");
}
說明:查看RegexKitLite源代碼,您會發現其實是對NSString的擴展,所以校驗的數據必須是NSString類型的。
1、在寫正則表達式時:所有的’\’都需要轉義,即:’\\’;
2、在很多JS的正則表達式可能是這樣寫,如:’/^\d{1,400}$/’,但是這樣的表達式Objective-C中並不能識別,通過實際調試得出,
應將其寫爲:’ ^\\d{1,400}$’(即:去掉表達式頭和尾的’/’)
只需將正則的內容改下即可,正則表達式並不是屬於哪種語言,但是卻很有必要學下。
基本使用的例子(更多信息參看 官方文檔 )
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
NSError *error = NULL;
matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
NSLog(@"matchedRange: %@" , NSStringFromRange(matchedRange));
// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
NSString *matchedString = [searchString substringWithRange:matchedRange];
NSLog(@"matchedString: '%@'" , matchedString);
// 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
NSString *searchString = @"This is neat.";
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
NSError *error = NULL;
matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));
// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
NSString *matchedString = [searchString substringWithRange:matchedRange];
NSLog(@"matchedString: '%@'", matchedString);
// 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
2.找到第一個匹配並返回一個NSString
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
NSLog(@"matchedString: '%@'" , matchedString);
// 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
NSString *searchString = @"This is neat.";
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
NSLog(@"matchedString: '%@'", matchedString);
// 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
3.查找和替換,加括號和概念和Python中的一樣,$1指代第一個括號中的內容
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"//b(//w+)//b" ;
NSString *replaceWithString = @"{$1}" ;
NSString *replacedString = NULL;
replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
//NSMutableString可以直接替換,並返回替換的次數
NSLog(@"replaced string: '%@'" , replacedString);
// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat." ];
NSString *regexString = @"//b(//w+)//b" ;
NSString *replaceWithString = @"{$1}" ;
NSUInteger replacedCount = 0UL;
replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
NSLog(@"count: %lu string: '%@'" , (u_long)replacedCount, mutableString);
// 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
NSString *searchString = @"This is neat.";
NSString *regexString = @"//b(//w+)//b";
NSString *replaceWithString = @"{$1}";
NSString *replacedString = NULL;
replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
//NSMutableString可以直接替換,並返回替換的次數
NSLog(@"replaced string: '%@'", replacedString);
// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat."];
NSString *regexString = @"//b(//w+)//b";
NSString *replaceWithString = @"{$1}";
NSUInteger replacedCount = 0UL;
replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);
// 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
4.用於拆分,返回一個拆分後的字符串數組
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"//s+" ;
NSArray *splitArray = NULL;
splitArray = [searchString componentsSeparatedByRegex:regexString];
// splitArray == { @"This", @"is", @"neat." }
NSLog(@"splitArray: %@" , splitArray);
NSString *searchString = @"This is neat.";
NSString *regexString = @"//s+";
NSArray *splitArray = NULL;
splitArray = [searchString componentsSeparatedByRegex:regexString];
// splitArray == { @"This", @"is", @"neat." }
NSLog(@"splitArray: %@", splitArray);
5.返回所有匹配的字符串數組,這個例子中雖然有多個括號,但是 componentsMatchedByRegex不管
NSString *searchString = @ "$10.23, $1024.42, $3099" ;
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
NSArray *matchArray = NULL;
matchArray = [searchString componentsMatchedByRegex:regexString];
// matchArray == { @"$10.23", @"$1024.42", @"$3099" };
NSLog(@"matchArray: %@" , matchArray);
6.返回所有匹配的字符串數組處理所有的括號
NSString *searchString = @"$10.23, $1024.42, $3099" ;
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
NSArray *capturesArray = NULL;
capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
NSLog(@"capturesArray: %@" , capturesArray);
輸出結果:
shell% ./capturesArray↵
2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
(
"$10.23" ,
"10.23" ,
10,
23
),
(
"$1024.42" ,
"1024.42" ,
1024,
42
),
(
"$3099" ,
3099,
3099,
""
)
)
NSString *searchString = @"$10.23, $1024.42, $3099";
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
NSArray *matchArray = NULL;
matchArray = [searchString componentsMatchedByRegex:regexString];
// matchArray == { @"$10.23", @"$1024.42", @"$3099" };
NSLog(@"matchArray: %@", matchArray);
6.返回所有匹配的字符串數組處理所有的括號
NSString *searchString = @"$10.23, $1024.42, $3099";
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
NSArray *capturesArray = NULL;
capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
NSLog(@"capturesArray: %@", capturesArray);
輸出結果:
shell% ./capturesArray↵
2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
(
"$10.23",
"10.23",
10,
23
),
(
"$1024.42",
"1024.42",
1024,
42
),
(
"$3099",
3099,
3099,
""
)
)