iphone正則表達(dá)式的簡(jiǎn)單使用

字號(hào):


    在 4.0 之后,系統(tǒng)就有了它自己的類(nsregularexpression,nsregularexpression)來使用正則表達(dá)式,,之前都是要添加第三方類庫 regexkitlite 來使用
    這兩個(gè)類的簡(jiǎn)單使用:
    nsstring *str = @3sdfh*odsi;
    //匹配第一個(gè)字符是數(shù)字
    nsregularexpression *regex1 = [nsregularexpression regularexpressionwithpattern:@bd.* options:0 error:nil];
    if (regex1 != nil) {
    nstextcheckingresult *result1 = [regex1 firstmatchinstring:str options:0 range:nsmakerange(0, [str length])];
    if (result1) {
    nslog(@第一個(gè)是數(shù)字);
    }else{
    nslog(@第一個(gè)不是數(shù)字);
    }
    }
    //匹配特殊字符 w (w是大寫)匹配任意不是字母,數(shù)字,下劃線,漢字的字符
    nsregularexpression *regex2 = [nsregularexpression regularexpressionwithpattern:@.*w.* options:0 error:nil];
    if (regex2) {
    nstextcheckingresult *result2 = [regex2 firstmatchinstring:str options:0 range:nsmakerange(0, [str length])];
    if (result2) {
    nslog(@有特殊字符);
    }else{
    nslog(@沒有特殊字符);
    }
    }