1. 正則表達式判斷url
NSString *a = @"http+:[^\\s]*";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", a];
BOOL checked = [emailTest evaluateWithObject:urlString];
下面是判斷url合法并抽取合法的url:
//NSRegularExpression類里面調用表達的方法需要傳遞一個NSError的參數。下面定義一個
NSError *error;
//http+:[^\\s]* 這個表達式是檢測一個網址的。
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當中截取數據
NSString *result=[urlString substringWithRange:resultRange];
//輸出結果
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類里面調用表達的方法需要傳遞一個NSError的參數。下面定義一個
NSError *error;
//http+:[^\\s]* 這個表達式是檢測一個網址的。
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當中截取數據
NSString *result=[urlString substringWithRange:resultRange];
//輸出結果
NSLog(@"%@",result);
}else {
NSLog(@"no result");
}
}