首先舉乙個例子:
匹配9-15個由字母/數字組成的字串的正規表示式:
nsstring * regex = @"^[a-za-z0-9]$";
nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex];
bool ismatch = [pred evaluatewithobject:txtfldphonenumber.text];
cocoa用nspredicate描述查詢的方式,原理類似於在資料庫中進行查詢
用between,in,beginwith,endwith,contains,like這些謂詞來構造
nspredicate,必要的時候使用self直接對自己進行匹配
//基本的查詢
nspredicate *predicate;
predicate = [nspredicate predicatewithformat: @"name == 'herbie'"];
bool match = [predicate evaluatewithobject: car];
nslog (@"%s", (match) ? "yes" : "no");
//在整個cars裡面迴圈比較
predicate = [nspredicate predicatewithformat: @"engine.horsepower > 150"];
nsarray *cars = [garage cars];
for (car *car in [garage cars])
}//輸出完整的資訊
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作為路徑名,因為它值代表值
//謂詞字元竄還支援c語言中一些常用的運算子
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"]);
//強大的陣列運算子
predicate = [nspredicate predicatewithformat:
@"engine.horsepower between "];
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運算子
predicate = [nspredicate predicatewithformat: @"name in "];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@", [results valueforkey: @"name"]);
predicate = [nspredicate predicatewithformat: @"self.name in "];
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@", [results valueforkey: @"name"]);
names = [cars valueforkey: @"name"];
predicate = [nspredicate predicatewithformat: @"self in "];
results = [names filteredarrayusingpredicate: predicate];//這裡限制了self的範圍
nslog (@"%@", results);
//beginswith,endswith,contains
//附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字元,cd表示什麼都不區分
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運算子(萬用字元)
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中正規表示式的使用
nsstring username aa5656fjalf67ghba8rt949aa3fa2s nsstring username 7pp7 使用正規表示式步驟 建立正規表示式物件,定義規則 利用正規表示式來測試相應字串 1 建立正規表示式 pattern 樣式 規則 表示只會查詢內部的乙個字元即...
IOS中正規表示式的使用
1 ios中通過nsregularexpression進行正規表示式匹配,例如 希望獲取字串開頭的數字,數字為1 3位,如下 nsstring drugdosagestr 555 毫克 劑量的數值最多三位 nserror error nsregularexpression regex nsregul...
ios中正規表示式的運用
注意 d正則法則在oc中表示式 d1.驗證使用者名稱和密碼 a za z w 正確格式 a z a z 0 9 組成,並且第乙個字必須為字母6 16位 2.驗證 號碼 d d 正確格式 x x xx 3.驗證身份證號 15位或18位數字 d d 5.只能輸入由數字和26個英文本母組成的字串 a za...