若何用SQL敕令檢查Mysql數據庫年夜小。本站提示廣大學習愛好者:(若何用SQL敕令檢查Mysql數據庫年夜小)文章只能為提供參考,不一定能成為您想要的結果。以下是若何用SQL敕令檢查Mysql數據庫年夜小正文
何謂正則表達式
正則表達式(regular expression),在盤算機迷信中,是指一個用來描寫或許婚配一系列相符某個句律例則的字符串的單個字符串。在許多文本編纂器或其他對象裡,正則表達式平日被用來檢索和/或調換那些相符某個形式的文本內容。正則表達式這個概念最後是由Unix中的對象軟件(例如sed和grep)普及開的。正則表達式平日縮寫成“regex”,雙數有regexp、regex,單數有regexps、regexes、regexen。
正則表達式構成
正則表達式有兩品種型的字符構成
第一種:用來婚配的字符,或許叫慣例字符
第二種:掌握字符或具有特別寄義的元字符
iphone 4.0今後就開端支撐正則表達式的應用了,在ios4.0中正則表達式的應用是應用NSRegularExpression類來挪用。
1. 上面一個簡略的應用正則表達式的一個例子:NSRegularExpression 類
-(void)parseString{ //組裝一個字符串,須要把外面的網址解析出來 NSString *urlString=@"sfdsfhttp://www.百度.com"; //NSRegularExpression類外面挪用表達的辦法須要傳遞一個NSError的參數。上面界說一個 NSError *error; //http+:[^\\s]* 這個表達式是檢測一個網址的。 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^\\s]*" options:0 error:&error]; if (regex != nil) { NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0range:NSMakeRange(0, [urlString length])]; if (firstMatch) { NSRange resultRange = [firstMatch rangeAtIndex:0]; //同等於 firstMatch.range --- 相婚配的規模 //從urlString傍邊截取數據 NSString *result=[urlString substringWithRange:resultRange]; //輸入成果 NSLog(@"%@",result); } } }
2.應用正則表達式來斷定
//初始化一個NSRegularExpression 對象,並設置檢測對象規模為:0-9 NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:0 error:nil]; if (regex2) {//對象停止婚配 NSTextCheckingResult *result2 = [regex2 firstMatchInString:textField.text options:0 range:NSMakeRange(0, [textField.text length])]; if (result2) { } }
1.斷定郵箱格局能否准確的代碼:NSPredicatel類
//應用正則表達式驗證
NSPredicatel類:重要用來指定過濾器的前提,該對象可以精確的描寫所需前提,對每一個對象經由過程謂詞停止挑選,斷定能否與前提相婚配。謂詞是指在盤算機中表現盤算真假值的函數。道理和用法都相似於SQL查詢中的where,感化相當於數據庫的過濾取。重要用於從聚集平分揀出相符前提的對象,也能夠用於字符串的正則婚配
-(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex]; return [emailTest evaluateWithObject:email]; }
2.婚配9-15個由字母/數字構成的字符串的正則表達式:
NSString * regex = @"^[A-Za-z0-9]{9,15}$"; 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]) { 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作為途徑名,由於它值代表值 //謂詞字符竄還支撐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 { 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運算符 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表現不辨別年夜小寫,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); //根本的查詢 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]) { 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作為途徑名,由於它值代表值 //謂詞字符竄還支撐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 { 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運算符 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表現不辨別年夜小寫,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中應用正則表達式NSRegularExpression 來驗證textfiled輸出的內容,願望年夜家愛好。