1. 程式人生 > >匹配URL,Email,電話號碼正則字串

匹配URL,Email,電話號碼正則字串

匹配URL字串

   NSMutableString * attStr = attString.mutableString;
    NSError *error = nil;;
    NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\
[email protected]
#$%^&*+?:_/=<>]*)?)"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr options:NSRegularExpressionCaseInsensitive error:&error]; if (error == nil) { NSArray *arrayOfAllMatches = [regex matchesInString:attStr options:0 range:NSMakeRange(0, [attStr length])]; for (NSTextCheckingResult *match in arrayOfAllMatches){ NSRange matchRange = match.range; // location=95, length=23 NSValue * valueRange = [NSValue valueWithRange:matchRange];//NSRange: {95, 23} NSString* substringForMatch = [attStr substringWithRange:matchRange];// https://www.google.com/ // 給url所在的字串新增前景顏色 [attString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)color.CGColor range:matchRange]; // 給url所在的字串新增下劃線 [attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:matchRange]; } }

匹配email字串

NSMutableString * attStr = attString.mutableString;
    NSError *error = nil;;
    NSString *regulaStr = @"[A-Z0-9a-z\\._%+-][email protected]([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr      options:NSRegularExpressionCaseInsensitive error:&error];
    if (error == nil)
    {
        NSArray *arrayOfAllMatches = [regex matchesInString:attStr
                                                    options:0
                                                      range:NSMakeRange(0, [attStr length])];
        for (NSTextCheckingResult *match in arrayOfAllMatches){
            NSRange matchRange = match.range; //location=22, length=16
            NSValue * valueRange = [NSValue valueWithRange:matchRange];//NSRange: {22, 16}
            
            NSString* substringForMatch = [attStr substringWithRange:matchRange];//
[email protected]
[attString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)color.CGColor range:matchRange]; [attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:matchRange]; } }

匹配電話號碼

 NSMutableString * attStr = attString.mutableString;
    NSError *error = nil;
    NSString *regulaStr = @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[358]+\\d{9}|\\d{8}|\\d{7}";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    if (error == nil)
    {
        NSArray *arrayOfAllMatches = [regex matchesInString:attStr
                                                    options:0
                                                      range:NSMakeRange(0, [attStr length])];
        
        for (NSTextCheckingResult *match in arrayOfAllMatches){
            
            NSRange matchRange = match.range;//location=22, length=8
            
            NSString* substringForMatch = [attStr substringWithRange:matchRange];//13456890000
            NSValue * valueRange = [NSValue valueWithRange:matchRange];
            [attString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)color.CGColor range:matchRange];
                [attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:matchRange];
         }
    }