使用正则提取url(iOS)

直接上代码:

 

  NSString *string = @"我是大大 www.baidu.com 咪咪咪";    

    NSError *error;

    NSString *regulaStr = @"\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>???“”‘’]))";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr                                                                          options:NSRegularExpressionCaseInsensitive error:&error];

    NSLog(@"error:%@", error);

    NSArray *arrayOfAllMatches = [regex matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, [string length])];

    for (NSTextCheckingResult *match in arrayOfAllMatches)

    {

        NSString* substringForMatch = [string substringWithRange:match.range];

        NSLog(@"substringForMatch");

        NSLog(@"%@", substringForMatch);

    }

posted @ 2015-08-31 17:22  平平君  阅读(645)  评论(0编辑  收藏  举报