a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Regex > 正文

iOS中使用正則表達式NSRegularExpression 來驗證textfiled輸入的內容

2020-03-16 21:04:19
字體:
來源:轉載
供稿:網友

一個正則表達式(regexp)是由元字符和文字數字的文本字符,或者“文字的”(abc,123,及其他)混合組合而成的文本模式。 該類型用于匹配文本字符——并附有匹配的結果,是成功還是失敗。 Regexps 主要用于規則文本匹配以及搜索和替換。

何謂正則表達式

正則表達式(regular expression),在計算機科學中,是指一個用來描述或者匹配一系列符合某個句法規則的字符串的單個字符串。在很多文本編輯器或其他工具里,正則表達式通常被用來檢索和/或替換那些符合某個模式的文本內容。正則表達式這個概念最初是由Unix中的工具軟件(例如sed和grep)普及開的。正則表達式通常縮寫成“regex”,單數有regexp、regex,復數有regexps、regexes、regexen。

正則表達式組成

正則表達式有兩種類型的字符組成

第一種:用來匹配的字符,或者叫常規字符

第二種:控制字符或具有特殊含義的元字符

iphone 4.0以后就開始支持正則表達式的使用了,在ios4.0中正則表達式的使用是使用NSRegularExpression類來調用。

1. 下面一個簡單的使用正則表達式的一個例子:NSRegularExpression 類

 

  1. -(void)parseString{ 
  2. //組裝一個字符串,需要把里面的網址解析出來 
  3. NSString *urlString=@"sfdsfhttp://www.baidu.com"
  4. //NSRegularExpression類里面調用表達的方法需要傳遞一個NSError的參數。下面定義一個 
  5. NSError *error; 
  6. //http+:[^//s]* 這個表達式是檢測一個網址的。 
  7. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^//s]*" options:0 error:&error]; 
  8. if (regex != nil) { 
  9. NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0range:NSMakeRange(0, [urlString length])]; 
  10. if (firstMatch) { 
  11. NSRange resultRange = [firstMatch rangeAtIndex:0]; //等同于 firstMatch.range --- 相匹配的范圍 
  12. //從urlString當中截取數據 
  13. NSString *result=[urlString substringWithRange:resultRange]; 
  14. //輸出結果 
  15. NSLog(@"%@",result); 

2.使用正則表達式來判斷

 

 
  1. //初始化一個NSRegularExpression 對象,并設置檢測對象范圍為:0-9  
  2. NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:0 error:nil]; 
  3. if (regex2) 
  4. {//對象進行匹配 
  5. NSTextCheckingResult *result2 = [regex2 firstMatchInString:textField.text options:0 range:NSMakeRange(0, [textField.text length])]; 
  6. if (result2) { 

1.判斷郵箱格式是否正確的代碼:NSPredicatel類

//利用正則表達式驗證

NSPredicatel類:主要用來指定過濾器的條件,該對象可以準確的描述所需條件,對每個對象通過謂詞進行篩選,判斷是否與條件相匹配。謂詞是指在計算機中表示計算真假值的函數。原理和用法都類似于SQL查詢中的where,作用相當于數據庫的過濾取。主要用于從集合中分揀出符合條件的對象,也可以用于字符串的正則匹配

 

 
  1. -(BOOL)isValidateEmail:(NSString *)email 
  2. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+//.[A-Za-z]{2,4}"
  3. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex]; 
  4. return [emailTest evaluateWithObject:email]; 

2.匹配9-15個由字母/數字組成的字符串的正則表達式:

 

 
  1. NSString * regex = @"^[A-Za-z0-9]{9,15}$"
  2. NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 
  3. BOOL isMatch = [pred evaluateWithObject:txtfldPhoneNumber.text]; 

Cocoa用NSPredicate描述查詢的方式,原理類似于在數據庫中進行查詢

用BETWEEN,IN,BEGINWITH,ENDWITH,CONTAINS,LIKE這些謂詞來構造NSPredicate,必要的時候使用SELF直接對自己進行匹配

 

 
  1. //基本的查詢  
  2. NSPredicate *predicate;  
  3. predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];  
  4. BOOL match = [predicate evaluateWithObject: car];  
  5. NSLog (@"%s", (match) ? "YES" : "NO");  
  6. //在整個cars里面循環比較  
  7. predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];  
  8. NSArray *cars = [garage cars];  
  9. for (Car *car in [garage cars]) {  
  10. if ([predicate evaluateWithObject: car]) {  
  11. NSLog (@"%@", car.name);  
  12. }  
  13. }  
  14. //輸出完整的信息  
  15. predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];  
  16. NSArray *results;  
  17. results = [cars filteredArrayUsingPredicate: predicate];  
  18. NSLog (@"%@", results);  
  19. //含有變量的謂詞  
  20. NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];  
  21. NSDictionary *varDict;  
  22. varDict = [NSDictionary dictionaryWithObjectsAndKeys:  
  23. @"Herbie", @"NAME", nil];  
  24. predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];  
  25. NSLog(@"SNORGLE: %@", predicate);  
  26. match = [predicate evaluateWithObject: car];  
  27. NSLog (@"%s", (match) ? "YES" : "NO");  
  28. //注意不能使用$VARIABLE作為路徑名,因為它值代表值  
  29. //謂詞字符竄還支持c語言中一些常用的運算符  
  30. predicate = [NSPredicate predicateWithFormat:  
  31. @"(engine.horsepower > 50) AND (engine.horsepower < 200)"];  
  32. results = [cars filteredArrayUsingPredicate: predicate];  
  33. NSLog (@"oop %@", results);  
  34. predicate = [NSPredicate predicateWithFormat: @"name < 'Newton'"];  
  35. results = [cars filteredArrayUsingPredicate: predicate];  
  36. NSLog (@"%@", [results valueForKey: @"name"]);  
  37. //強大的數組運算符  
  38. predicate = [NSPredicate predicateWithFormat:  
  39. @"engine.horsepower BETWEEN { 50, 200 }"];  
  40. results = [cars filteredArrayUsingPredicate: predicate];  
  41. NSLog (@"%@", results);  
  42. NSArray *betweens = [NSArray arrayWithObjects:  
  43. [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];  
  44. predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];  
  45. results = [cars filteredArrayUsingPredicate: predicate];  
  46. NSLog (@"%@", results);  
  47. predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];  
  48. varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];  
  49. predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];  
  50. results = [cars filteredArrayUsingPredicate: predicate];  
  51. NSLog (@"%@", results);  
  52. //IN運算符  
  53. predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
  54. results = [cars filteredArrayUsingPredicate: predicate];  
  55. NSLog (@"%@", [results valueForKey: @"name"]);  
  56. predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
  57. results = [cars filteredArrayUsingPredicate: predicate];  
  58. NSLog (@"%@", [results valueForKey: @"name"]);  
  59. names = [cars valueForKey: @"name"];  
  60. predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
  61. results = [names filteredArrayUsingPredicate: predicate];//這里限制了SELF的范圍  
  62. NSLog (@"%@", results);  
  63. //BEGINSWITH,ENDSWITH,CONTAINS  
  64. //附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字符,cd表示什么都不區分  
  65. predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];  
  66. results = [cars filteredArrayUsingPredicate: predicate];  
  67. NSLog (@"%@", results);  
  68. predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];  
  69. results = [cars filteredArrayUsingPredicate: predicate];  
  70. NSLog (@"%@", results);  
  71. predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];  
  72. results = [cars filteredArrayUsingPredicate: predicate];  
  73. NSLog (@"%@", results);  
  74. //LIKE運算符(通配符)  
  75. predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];  
  76. results = [cars filteredArrayUsingPredicate: predicate];  
  77. NSLog (@"%@", results);  
  78. predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];  
  79. results = [cars filteredArrayUsingPredicate: predicate];  
  80. NSLog (@"%@", results);  
  81. //基本的查詢 
  82. NSPredicate *predicate; 
  83. predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"]; 
  84. BOOL match = [predicate evaluateWithObject: car]; 
  85. NSLog (@"%s", (match) ? "YES" : "NO"); 
  86. //在整個cars里面循環比較 
  87. predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"]; 
  88. NSArray *cars = [garage cars]; 
  89. for (Car *car in [garage cars]) { 
  90. if ([predicate evaluateWithObject: car]) { 
  91. NSLog (@"%@", car.name); 
  92. //輸出完整的信息 
  93. predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"]; 
  94. NSArray *results; 
  95. results = [cars filteredArrayUsingPredicate: predicate]; 
  96. NSLog (@"%@", results); 
  97. //含有變量的謂詞 
  98. NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"]; 
  99. NSDictionary *varDict; 
  100. varDict = [NSDictionary dictionaryWithObjectsAndKeys: 
  101. @"Herbie", @"NAME", nil]; 
  102. predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict]; 
  103. NSLog(@"SNORGLE: %@", predicate); 
  104. match = [predicate evaluateWithObject: car]; 
  105. NSLog (@"%s", (match) ? "YES" : "NO"); 
  106. //注意不能使用$VARIABLE作為路徑名,因為它值代表值 
  107. //謂詞字符竄還支持c語言中一些常用的運算符 
  108. predicate = [NSPredicate predicateWithFormat: 
  109. @"(engine.horsepower > 50) AND (engine.horsepower < 200)"]; 
  110. results = [cars filteredArrayUsingPredicate: predicate]; 
  111. NSLog (@"oop %@", results); 
  112. predicate = [NSPredicate predicateWithFormat: @"name < 'Newton'"]; 
  113. results = [cars filteredArrayUsingPredicate: predicate]; 
  114. NSLog (@"%@", [results valueForKey: @"name"]); 
  115. //強大的數組運算符 
  116. predicate = [NSPredicate predicateWithFormat: 
  117. @"engine.horsepower BETWEEN { 50, 200 }"]; 
  118. results = [cars filteredArrayUsingPredicate: predicate]; 
  119. NSLog (@"%@", results); 
  120. NSArray *betweens = [NSArray arrayWithObjects: 
  121. [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil]; 
  122. predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens]; 
  123. results = [cars filteredArrayUsingPredicate: predicate]; 
  124. NSLog (@"%@", results); 
  125. predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"]; 
  126. varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil]; 
  127. predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict]; 
  128. results = [cars filteredArrayUsingPredicate: predicate]; 
  129. NSLog (@"%@", results); 
  130. //IN運算符 
  131. predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; 
  132. results = [cars filteredArrayUsingPredicate: predicate]; 
  133. NSLog (@"%@", [results valueForKey: @"name"]); 
  134. predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; 
  135. results = [cars filteredArrayUsingPredicate: predicate]; 
  136. NSLog (@"%@", [results valueForKey: @"name"]); 
  137. names = [cars valueForKey: @"name"]; 
  138. predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; 
  139. results = [names filteredArrayUsingPredicate: predicate];//這里限制了SELF的范圍 
  140. NSLog (@"%@", results); 
  141. //BEGINSWITH,ENDSWITH,CONTAINS 
  142. //附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字符,cd表示什么都不區分 
  143. predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"]; 
  144. results = [cars filteredArrayUsingPredicate: predicate]; 
  145. NSLog (@"%@", results); 
  146. predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"]; 
  147. results = [cars filteredArrayUsingPredicate: predicate]; 
  148. NSLog (@"%@", results); 
  149. predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"]; 
  150. results = [cars filteredArrayUsingPredicate: predicate]; 
  151. NSLog (@"%@", results); 
  152. //LIKE運算符(通配符) 
  153. predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"]; 
  154. results = [cars filteredArrayUsingPredicate: predicate]; 
  155. NSLog (@"%@", results); 
  156. predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"]; 
  157. results = [cars filteredArrayUsingPredicate: predicate]; 
  158. NSLog (@"%@", results); 

以上就是小編給大家分享的iOS中使用正則表達式NSRegularExpression 來驗證textfiled輸入的內容,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美视频三区 | 日韩成人一级片 | 欧美一级在线 | 男女羞羞视频免费在线观看 | 高潮毛片又色又爽免费 | 国产精品欧美一区二区三区不卡 | 最新av片 | 91在线资源| 日本色站 | 青青国产在线 | 日韩av免费在线观看 | 国产精一区二区 | 美女隐私视频黄www曰本 | 在线观看免费毛片视频 | 欧美国产日韩一区二区三区 | 激情视频网站 | 韩日精品在线观看 | 亚洲免费在线观看 | 色综合激情 | 激情国产| 北条麻妃99精品青青久久 | 欧美三日本三级三级在线播放 | 欧美一级欧美三级在线观看 | 精品在线视频免费观看 | 成人黄视频在线观看 | 手机在线成人免费视频 | 在线播放91 | www久久精品 | 欧美韩一区二区 | 成人av电影免费在线观看 | 天天干天天操 | 日韩性视频 | 欧美一区二区 | 久久亚洲视频 | 亚洲成人一区二区三区 | 97在线免费观看 | 伊人狠狠干| 久久国产精品成人免费观看的软件 | 青青草网 | 一区二区三区国产精品 | 亚洲欧美另类图片 |