好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > iOS中使用正則表達(dá)式NSRegularExpression 來驗(yàn)證textfiled輸入的內(nèi)容

iOS中使用正則表達(dá)式NSRegularExpression 來驗(yàn)證textfiled輸入的內(nèi)容

熱門標(biāo)簽:地圖標(biāo)注字母的軟件 地圖標(biāo)注商戶中心要收錢多少 線上教育ai外呼系統(tǒng) css百度地圖標(biāo)注位置顯示 實(shí)用地圖標(biāo)注app 鄂州人工智能電銷機(jī)器人軟件 400免費(fèi)電話去哪申請 菏澤智能ai電銷機(jī)器人銷售公司 宿遷智能外呼系統(tǒng)供應(yīng)商

何謂正則表達(dá)式

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

正則表達(dá)式組成

正則表達(dá)式有兩種類型的字符組成

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

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

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

1. 下面一個(gè)簡單的使用正則表達(dá)式的一個(gè)例子:NSRegularExpression 類

-(void)parseString{
//組裝一個(gè)字符串,需要把里面的網(wǎng)址解析出來
NSString *urlString=@"sfdsfhttp://www.baidu.com";
//NSRegularExpression類里面調(diào)用表達(dá)的方法需要傳遞一個(gè)NSError的參數(shù)。下面定義一個(gè)
 NSError *error;
//http+:[^\\s]* 這個(gè)表達(dá)式是檢測一個(gè)網(wǎng)址的。
  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當(dāng)中截取數(shù)據(jù)
  NSString *result=[urlString substringWithRange:resultRange];
  //輸出結(jié)果
  NSLog(@"%@",result);
  }
  }
}

2.使用正則表達(dá)式來判斷

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

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

//利用正則表達(dá)式驗(yàn)證

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

-(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個(gè)由字母/數(shù)字組成的字符串的正則表達(dá)式:

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

Cocoa用NSPredicate描述查詢的方式,原理類似于在數(shù)據(jù)庫中進(jìn)行查詢

用BETWEEN,IN,BEGINWITH,ENDWITH,CONTAINS,LIKE這些謂詞來構(gòu)造NSPredicate,必要的時(shí)候使用SELF直接對自己進(jìn)行匹配

//基本的查詢 
NSPredicate *predicate; 
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"]; 
  BOOL match = [predicate evaluateWithObject: car]; 
  NSLog (@"%s", (match) ? "YES" : "NO"); 
//在整個(gè)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");
//在整個(gè)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)容,希望大家喜歡。

您可能感興趣的文章:
  • iOS App開發(fā)中Objective-C使用正則表達(dá)式進(jìn)行匹配的方法
  • 正則表達(dá)式在IOS中的應(yīng)用及IOS中三種正則表達(dá)式的使用與比較
  • iOS 正則表達(dá)式判斷手機(jī)號碼、固話
  • iOS 正則表達(dá)式判斷純數(shù)字及匹配11位手機(jī)號碼的方法
  • IOS開發(fā)常用的正則表達(dá)式
  • iOS 中使用正則表達(dá)式判斷身份證格式及銀行卡號格式是否正確(推薦)
  • iOS正則表達(dá)式驗(yàn)證手機(jī)號、郵箱、身份證號等
  • ios利用正則表達(dá)式判斷手機(jī)號碼格式是否正確的實(shí)例
  • Objective-C中利用正則去除非數(shù)字字母漢字方法實(shí)例

標(biāo)簽:三亞 池州 梅州 六安 綿陽 咸陽 鞍山 恩施

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《iOS中使用正則表達(dá)式NSRegularExpression 來驗(yàn)證textfiled輸入的內(nèi)容》,本文關(guān)鍵詞  iOS,中,使用,正則,表達(dá)式,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《iOS中使用正則表達(dá)式NSRegularExpression 來驗(yàn)證textfiled輸入的內(nèi)容》相關(guān)的同類信息!
  • 本頁收集關(guān)于iOS中使用正則表達(dá)式NSRegularExpression 來驗(yàn)證textfiled輸入的內(nèi)容的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 肉文调教小说| 动漫美女被日| 亚洲 日韩 欧美 综合 热| 变态调教sm冷先生在线 | 爱的迷宫| 三人行0.5夹心攻受| 成人影院午夜男女爽爽爽软件| 99精品一区二区三区| 污网站免费观看在线高清| 亚洲乱码国产乱码精品精98| 五月天丁香花婷婷| chinaese中国女人厕所小便| 女同啪啪免费网站www| 欧美 国产 小说 另类| 胯下的青春男男高Hbl| 国产69久久久欧美黑人A片| 操白虎美女| 女朋友下边粉嫩粉嫩的怎么办呢| 91丨人妻丨丝袜一区| 女的越疼男的越往里寨的视频| 色多网站免费视频| 在线网站污| 国产第一页在线播放| 国产精品稀缺呦系列在线| 国外留学和5个老外做| 无码国产精品久久一区免费下春药| 在线播放国产不卡免费视频| 极品粉嫩軳600张美女图片| 久久香蕉国产线看观看8青草 | 97国产熟妇视频二区| 女人被老外躁得好爽免费视频 | 宫姝未删减完整版在线阅读| 色戒在线观看电影| h揉捏娇喘乳娇喘H| 亚洲精品偷拍一卡不卡二卡| 草莓视频污黄色| 丰满的已婚女人| 99日韩精品| 四季常藍,百搭樹種| 国产精品丝袜黑色高跟鞋V18| 亚洲欧美乱|