[iOS] Url 정규식으로 추출

정규식 참고용 (Objective C 용)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma mark - 정규식으로 http link 추출
- (NSMutableArray *)getRegularHttpLink:(NSString *)string
{
    NSMutableArray *resultList = [[NSMutableArray alloc] init];
    if (string) {
        NSError *error = nil;
        NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
        NSArray *matches = [detector matchesInString:string
                                             options:0
                                               range:NSMakeRange(0, [string length])];
// 문자열에 포함된 Url 정보 빼내기   
        for (NSTextCheckingResult *match in matches) {
            if ([match resultType] == NSTextCheckingTypeLink) {
                NSURL *url = [match URL];
                [resultList addObject:url];
            }
        }
    }  
    return resultList;
}
cs

댓글