iOS正則表達式(一)

來源:互聯網
上載者:User

標籤:length   ios開發   port   UI   cas   orm   index   target   last   

什麼是正則表達式?
正則表達式是對字串操作的一種邏輯公式。
作用?
在iOS開發中我們通常使用正則表達式來匹配給定的字串是否符合我們的商務邏輯,比方說使用者注冊帳號僅僅能是手機號或者郵箱等。我們還能夠使用正則表達式來從一段字串其中截取我們須要的字串,比方說網頁原始碼中我們須要截取某些個圖片地址等。總得來說。在iOS開發中正則表達式的作用有兩點:    1. 檢測給定的字串是否符合商務邏輯    2. 從目標字串中擷取我們想要的特定字串
怎樣在OC中使用正則表達式?1.利用NSPredicate(謂詞)匹配
 NSString * targetString = @"13534341234"; NSString * [email protected]"^[1][3578]+\\d{9}"; NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularExpression]; BOOL flag = [pre evaluateWithObject:targetString]; if (flag) {    NSLog(@"是手機號碼"); }else{    NSLog(@"不是手機號碼"); }
    如上代碼所看到的為簡單的正則表達式在iOS中的使用方式,意思為:使用者輸入的字串是否為手機號碼。    其中 `targetString` 為用書輸入字串,`regularExpression` 為正則表達式文法,`NSPredicate`為OC中查詢過濾類
2.利用rangeOfString:option:直接尋找目標字串
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.hao123.com"] encoding:4 error:nil];        NSRange range = [string rangeOfString:@"(<img.*?/>)" options:NSRegularExpressionSearch];NSString *subStr = [string substringWithRange:range]; NSLog(@"subStr = %@",subStr);
如上代碼就是從網頁中過濾出我們的圖片標籤。刪除結果為:`<img class="img-hook" src="http://s0.hao123img.com/res/img/logo/logonew1.png" width="210" height="60" />`
3.使用正則表達式類
    NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.hao123.com"] encoding:4 error:nil];     NSString *pattern = @"<img src=(.*?)>";     NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators error:nil];        NSTextCheckingResult *checkResult = [regex firstMatchInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];        NSString *result = [string substringWithRange:[checkResult rangeAtIndex:0]];        NSLog(@"result = %@",result);
此代碼和上面代碼一樣是找到網址中一個圖片標籤,那麼要講網址中全部的圖片標籤都找到,怎樣寫?
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.hao123.com"] encoding:4 error:nil];     NSString *pattern = @"<img src=(.*?)>";     NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators error:nil];NSArray *theArray = [regex matchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length)];        for ( NSTextCheckingResult *ele in theArray) {            NSString *theLastString = [string substringWithRange:[ele rangeAtIndex:0]];            NSLog(@"theLastString = %@",theLastString);        }
那麼問題來了。上面寫的例如以下字串是個什麼鬼?
NSString * [email protected]"^[1][3578]+\\d{9}"NSString *pattern = @"<img src=(.*?)>";

對,這就是我們的正則表達式。那麼正則表達式的文法又是怎樣的呢?且聽下回分解!

我的優酷空間,有視頻教程哦,歡迎點擊。

iOS正則表達式(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.