获取顶级域名方法

项目需求,获取网页的顶级域名 eg:www.baidu.com 顶级域名:baidu.com

- (NSString *)getCustomHostName:(NSString *)absoluteURL {
    NSString *reg = @"(?i)^https?://(?:\\w+\\.)*?(\\w*\\.(?:com\\.cn|cn|com|net))[\\\\/]*";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:reg options:0 error:&error];
    if (regex != nil) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:absoluteURL options:0 range:NSMakeRange(0, [absoluteURL length])];
        if (firstMatch) {
            NSRange resultRange = [firstMatch rangeAtIndex:1];
            NSString *result = [absoluteURL substringWithRange:resultRange];
            return result;
        }else {
            return @"";
        }
    }
    return @"";
}

你可能感兴趣的:(获取顶级域名方法)