正則表達(dá)式(regular expression),在計(jì)算機(jī)科學(xué)中,是指一個用來描述或者匹配一系列符合某個句法規(guī)則的字符串的單個字符串。在很多文本編輯器或其他工具里,正則表達(dá)式通常被用來檢索和/或替換那些符合某個模式的文本內(nèi)容。正則表達(dá)式這個概念最初是由Unix中的工具軟件(例如sed和grep)普及開的。正則表達(dá)式通??s寫成“regex”,單數(shù)有regexp、regex,復(fù)數(shù)有regexps、regexes、regexen。
iphone 4.0以后就開始支持正則表達(dá)式的使用了,在ios4.0中正則表達(dá)式的使用是使用NSRegularExpression類來調(diào)用。
NSPredicatel類:主要用來指定過濾器的條件,該對象可以準(zhǔn)確的描述所需條件,對每個對象通過謂詞進(jìn)行篩選,判斷是否與條件相匹配。謂詞是指在計(jì)算機(jī)中表示計(jì)算真假值的函數(shù)。原理和用法都類似于SQL查詢中的where,作用相當(dāng)于數(shù)據(jù)庫的過濾取。主要用于從集合中分揀出符合條件的對象,也可以用于字符串的正則匹配
用BETWEEN,IN,BEGINWITH,ENDWITH,CONTAINS,LIKE這些謂詞來構(gòu)造NSPredicate,必要的時候使用SELF直接對自己進(jìn)行匹配
//基本的查詢
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
//在整個cars里面循環(huán)比較
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *cars = [garage cars];
for (Car *car in [garage cars]) {
if ([predicate evaluateWithObject: car]) {
NSLog (@"%@", car.name);
}
}
//輸出完整的信息
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *results;
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//含有變量的謂詞
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
NSDictionary *varDict;
varDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Herbie", @"NAME", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
NSLog(@"SNORGLE: %@", predicate);
match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
//注意不能使用$VARIABLE作為路徑名,因?yàn)樗荡碇?
//謂詞字符竄還支持c語言中一些常用的運(yùn)算符
predicate = [NSPredicate predicateWithFormat:
@"(engine.horsepower > 50) AND (engine.horsepower 200)"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"oop %@", results);
predicate = [NSPredicate predicateWithFormat: @"name 'Newton'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
//強(qiáng)大的數(shù)組運(yùn)算符
predicate = [NSPredicate predicateWithFormat:
@"engine.horsepower BETWEEN { 50, 200 }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSArray *betweens = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];
varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//IN運(yùn)算符
predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
names = [cars valueForKey: @"name"];
predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [names filteredArrayUsingPredicate: predicate];//這里限制了SELF的范圍
NSLog (@"%@", results);
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符號,[c],[d],[cd],c表示不區(qū)分大小寫,d表示不區(qū)分發(fā)音字符,cd表示什么都不區(qū)分
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//LIKE運(yùn)算符(通配符)
predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//基本的查詢
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
//在整個cars里面循環(huán)比較
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *cars = [garage cars];
for (Car *car in [garage cars]) {
if ([predicate evaluateWithObject: car]) {
NSLog (@"%@", car.name);
}
}
//輸出完整的信息
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *results;
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//含有變量的謂詞
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
NSDictionary *varDict;
varDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Herbie", @"NAME", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
NSLog(@"SNORGLE: %@", predicate);
match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
//注意不能使用$VARIABLE作為路徑名,因?yàn)樗荡碇?
//謂詞字符竄還支持c語言中一些常用的運(yùn)算符
predicate = [NSPredicate predicateWithFormat:
@"(engine.horsepower > 50) AND (engine.horsepower 200)"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"oop %@", results);
predicate = [NSPredicate predicateWithFormat: @"name 'Newton'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
//強(qiáng)大的數(shù)組運(yùn)算符
predicate = [NSPredicate predicateWithFormat:
@"engine.horsepower BETWEEN { 50, 200 }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSArray *betweens = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];
varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//IN運(yùn)算符
predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
names = [cars valueForKey: @"name"];
predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [names filteredArrayUsingPredicate: predicate];//這里限制了SELF的范圍
NSLog (@"%@", results);
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符號,[c],[d],[cd],c表示不區(qū)分大小寫,d表示不區(qū)分發(fā)音字符,cd表示什么都不區(qū)分
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//LIKE運(yùn)算符(通配符)
predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
以上就是小編給大家分享的iOS中使用正則表達(dá)式NSRegularExpression 來驗(yàn)證textfiled輸入的內(nèi)容,希望大家喜歡。