Using NSRegularExpression to extract URLs on the iPhone

后端 未结 5 596
耶瑟儿~
耶瑟儿~ 2020-11-30 09:16

I\'m using the following code on my iPhone app, taken from here to extract all URLs from striped .html code.

I\'m only being able to extract the first URL, but I nee

相关标签:
5条回答
  • 2020-11-30 09:35

    Try NSDataDetector

    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [linkDetector matchesInString:text options:0 range:NSMakeRange(0, [text length])];
    
    0 讨论(0)
  • 2020-11-30 09:47

    With NSDataDetector using Swift :

    let types: NSTextCheckingType = .Link
    var error : NSError?
    
    let detector = NSDataDetector(types: types.rawValue, error: &error)        
    var matches = detector!.matchesInString(text, options: nil, range: NSMakeRange(0, count(text)))
    
    for match in matches {
       println(match.URL!)
    }
    

    Using Swift 2.0:

    let text = "http://www.google.com. http://www.bla.com"
    let types: NSTextCheckingType = .Link
    
    let detector = try? NSDataDetector(types: types.rawValue)
    
    guard let detect = detector else {
       return
    }
    
    let matches = detect.matchesInString(text, options: .ReportCompletion, range: NSMakeRange(0, text.characters.count))
    
    for match in matches {
       print(match.URL!)
    }
    

    Using Swift 3.0

    let text = "http://www.google.com. http://www.bla.com"
    let types: NSTextCheckingResult.CheckingType = .link
    
    let detector = try? NSDataDetector(types: types.rawValue)
    
    let matches = detector?.matches(in: text, options: .reportCompletion, range: NSMakeRange(0, text.characters.count))
    
    for match in matches! {
       print(match.url!)
    }
    
    0 讨论(0)
  • 2020-11-30 09:50

    The method matchesInString:options:range: returns an array of NSTextCheckingResult objects. You can use fast enumeration to iterate through the array, pull out the substring of each match from your original string, and add the substring to a new array.

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?" options:NSRegularExpressionCaseInsensitive error:&error];
    
    NSArray *arrayOfAllMatches = [regex matchesInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])];
    
    NSMutableArray *arrayOfURLs = [[NSMutableArray alloc] init];
    
    for (NSTextCheckingResult *match in arrayOfAllMatches) {    
        NSString* substringForMatch = [httpLine substringWithRange:match.range];
        NSLog(@"Extracted URL: %@",substringForMatch);
    
        [arrayOfURLs addObject:substringForMatch];
    }
    
    // return non-mutable version of the array
    return [NSArray arrayWithArray:arrayOfURLs];
    
    0 讨论(0)
  • 2020-11-30 09:54

    to get all links from a given string

    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
    NSString *someString = @"www.facebook.com/link/index.php This is a sample www.google.com of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
    
    NSArray *matches = [expression matchesInString:someString options:NSMatchingCompleted range:NSMakeRange(0, someString.length)];
    for (NSTextCheckingResult *result in matches) {
            NSString *url = [someString substringWithRange:result.range];
            NSLog(@"found url:%@", url);
    }
    
    0 讨论(0)
  • 2020-11-30 09:57

    I found myself so nauseated by the complexity of this simple operation ("match ALL the substrings") that I made a little library I am humbly calling Unsuck which adds some sanity to NSRegularExpression in the form of from and allMatches methods. Here's how you'd use them:

    NSRegularExpression *re = [NSRegularExpression from: @"(?i)\\b(https?://.*)\\b"]; // or whatever your favorite regex is; Hossam's seems pretty good
    NSArray *matches = [re allMatches:httpLine];
    

    Please check out the unsuck source code on github and tell me all the things I did wrong :-)

    Note that (?i) makes it case insensitive so you don't need to specify NSRegularExpressionCaseInsensitive.

    0 讨论(0)
提交回复
热议问题