正則表達式-03 更多的方式

這篇文章主要是學習剩下的一些常見的規則語法

博文代碼(點擊下載)

^: 非運算 a[^b] 除了b以爲的字符 或者^a 以a開頭 
\d:代表一個數字,等同於[0-9] 
\D:代表⾮數字,等同於[^0-9]
\s:代表換行符、Tab製表符等空⽩字符
\S:代表⾮空白字符 
\w:匹配任何字類字符,包括下劃線。與“[A-Za-z0-9_]”等效。 
\W:⾮\w ,等同於[^\w]

^的語法代碼:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk.com";

        NSString* pattern = @"h[^ttp]";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:h[^ttp]————不以http開頭的字符

RegularExpression03[3261:303] ho

代碼:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk.com";

        NSString* pattern = @"^http";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:沒有中括號括着,就是以^後面字符串開頭.所以^http,就是要以http開頭的字符

RegularExpression03[3276:303] http

\d的語法代碼:代表一個數字,等同於[0-9]

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk123.com";

        NSString* pattern = @"\\d";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:

RegularExpression03[3288:303] 1
RegularExpression03[3288:303] 2
RegularExpression03[3288:303] 3

\D的語法代碼:代表⾮數字,等同於[^0-9]

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk123.com";

        NSString* pattern = @"\\D";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:

RegularExpression03[3297:303] h
RegularExpression03[3297:303] t
RegularExpression03[3297:303] t
RegularExpression03[3297:303] p
RegularExpression03[3297:303] :
RegularExpression03[3297:303] /
RegularExpression03[3297:303] /
RegularExpression03[3297:303] w
RegularExpression03[3297:303] w
RegularExpression03[3297:303] w
RegularExpression03[3297:303] .
RegularExpression03[3297:303] h
RegularExpression03[3297:303] o
RegularExpression03[3297:303] u
RegularExpression03[3297:303] s
RegularExpression03[3297:303] n
RegularExpression03[3297:303] k
RegularExpression03[3297:303] .
RegularExpression03[3297:303] c
RegularExpression03[3297:303] o
RegularExpression03[3297:303] m

\s的語法代碼:代表換行符、Tab製表符等空⽩字符

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\s";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:

RegularExpression03[3317:303]  

\S的語法代碼:代表⾮空白字符

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\S";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:

RegularExpression03[3297:303] h
RegularExpression03[3297:303] t
RegularExpression03[3297:303] t
RegularExpression03[3297:303] p
RegularExpression03[3297:303] :
RegularExpression03[3297:303] /
RegularExpression03[3297:303] /
RegularExpression03[3297:303] w
RegularExpression03[3297:303] w
RegularExpression03[3297:303] w
RegularExpression03[3297:303] .
RegularExpression03[3297:303] h
RegularExpression03[3297:303] o
RegularExpression03[3297:303] u
RegularExpression03[3297:303] s
RegularExpression03[3297:303] n
RegularExpression03[3297:303] k
RegularExpression03[3297:303] 1
RegularExpression03[3297:303] 2
RegularExpression03[3297:303] 3
RegularExpression03[3297:303] .
RegularExpression03[3297:303] c
RegularExpression03[3297:303] o
RegularExpression03[3297:303] m

\w的語法代碼:匹配任何字類字符,包括下劃線。與“[A-Za-z0-9_]”等效。

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\w";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行效果:

RegularExpression03[3297:303] h
RegularExpression03[3297:303] t
RegularExpression03[3297:303] t
RegularExpression03[3297:303] p
RegularExpression03[3297:303] :
RegularExpression03[3297:303] /
RegularExpression03[3297:303] /
RegularExpression03[3297:303] w
RegularExpression03[3297:303] w
RegularExpression03[3297:303] w
RegularExpression03[3297:303] .
RegularExpression03[3297:303] h
RegularExpression03[3297:303] o
RegularExpression03[3297:303] u
RegularExpression03[3297:303] s
RegularExpression03[3297:303] n
RegularExpression03[3297:303] k
RegularExpression03[3297:303] 1
RegularExpression03[3297:303] 2
RegularExpression03[3297:303] 3
RegularExpression03[3297:303] .
RegularExpression03[3297:303] c
RegularExpression03[3297:303] o
RegularExpression03[3297:303] m

\W的語法代碼:⾮\w ,等同於[^\w]

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\W";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"檢索無結果");

        }
    }
    return 0;
}

運行結果:

RegularExpression03[3342:303] :
RegularExpression03[3342:303] /
RegularExpression03[3342:303] /
RegularExpression03[3342:303] .
RegularExpression03[3342:303]  
RegularExpression03[3342:303] .
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章