1. 正則表達(dá)式判斷url
NSString *a = @"http+:[^\\s]*";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", a];
BOOL checked = [emailTest evaluateWithObject:urlString];
下面是判斷url合法并抽取合法的url:
//NSRegularExpression類里面調(diào)用表達(dá)的方法需要傳遞一個NSError的參數(shù)。下面定義一個
NSError *error;
//http+:[^\\s]* 這個表達(dá)式是檢測一個網(wǎng)址的。
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^\\s]*" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
NSTextCheckingResult *firstMacth = [regex ]
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
//從urlString當(dāng)中截取數(shù)據(jù)
NSString *result=[urlString substringWithRange:resultRange];
//輸出結(jié)果
NSLog(@"%@",result);
}else {
NSLog(@"no result");
}
}
NSString *a = @"http+:[^\\s]*";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", a];
BOOL checked = [emailTest evaluateWithObject:urlString];
下面是判斷url合法并抽取合法的url:
//NSRegularExpression類里面調(diào)用表達(dá)的方法需要傳遞一個NSError的參數(shù)。下面定義一個
NSError *error;
//http+:[^\\s]* 這個表達(dá)式是檢測一個網(wǎng)址的。
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^\\s]*" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
NSTextCheckingResult *firstMacth = [regex ]
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
//從urlString當(dāng)中截取數(shù)據(jù)
NSString *result=[urlString substringWithRange:resultRange];
//輸出結(jié)果
NSLog(@"%@",result);
}else {
NSLog(@"no result");
}
}