2010-12-29 6 views
1

テキストの本文にURLを見つけるアルゴリズムを考え出しています。私は現在、次のコードを持っている(これは、ダウン私の座ったとコードをそれをハックと私はより良い方法がなければならない知っている):本文のURLを検索するためのアルゴリズムを改善しました。-obj-c

statusText.text = @"http://google.com http://www.apple.com www.joshholat.com"; 

NSMutableArray *urlLocations = [[NSMutableArray alloc] init]; 

NSRange currentLocation = NSMakeRange(0, statusText.text.length); 
for (int x = 0; x < statusText.text.length; x++) { 
    currentLocation = [[statusText.text substringFromIndex:(x + currentLocation.location)] rangeOfString:@"http://"]; 
    if (currentLocation.location > statusText.text.length) break; 
    [urlLocations addObject:[NSNumber numberWithInt:(currentLocation.location + x)]]; 
} 
currentLocation = NSMakeRange(0, statusText.text.length); 
for (int x = 0; x < statusText.text.length; x++) { 
    currentLocation = [[statusText.text substringFromIndex:(x + currentLocation.location)] rangeOfString:@"http://www."]; 
    if (currentLocation.location > statusText.text.length) break; 
    [urlLocations addObject:[NSNumber numberWithInt:(currentLocation.location + x)]]; 
} 
currentLocation = NSMakeRange(0, statusText.text.length); 
for (int x = 0; x < statusText.text.length; x++) { 
    currentLocation = [[statusText.text substringFromIndex:(x + currentLocation.location)] rangeOfString:@" www." options:NSLiteralSearch]; 
    if (currentLocation.location > statusText.text.length) break; 
    [urlLocations addObject:[NSNumber numberWithInt:(currentLocation.location + 1 + x)]]; 
} 

//Get rid of any duplicate locations 
NSSet *uniqueElements = [NSSet setWithArray:urlLocations]; 
[urlLocations release]; 
NSArray *finalURLLocations = [[NSArray alloc] init]; 
finalURLLocations = [uniqueElements allObjects]; 

//Parse out the URLs of each of the locations 
for (int x = 0; x < [finalURLLocations count]; x++) { 
    NSRange temp = [[statusText.text substringFromIndex:[[finalURLLocations objectAtIndex:x] intValue]] rangeOfString:@" "]; 
    int length = temp.location + [[finalURLLocations objectAtIndex:x] intValue]; 
    if (temp.location > statusText.text.length) length = statusText.text.length; 
    length = length - [[finalURLLocations objectAtIndex:x] intValue]; 
    NSLog(@"URL: %@", [statusText.text substringWithRange:NSMakeRange([[finalURLLocations objectAtIndex:x] intValue], length)]); 
} 

それは正規表現の使用を経由して改善することができるように私は感じをか何か。これを改善するための助けがあれば幸いです。

答えて

5

iOS 4.0以降をターゲットにしている場合は、アップルに任せて、組み込みのデータ検出器を使用するようにしてください。​​オプションを使用してNSDataDetectorのインスタンスを作成し、文字列の上で実行します。 documentation for NSDataDetectorには、クラスの使用に関するいくつかの良い例があります。

あなたが/何らかの理由でデータ検出器を使用することはできませんしていない場合は、ジョン・グルーバーは、数ヶ月前にURLを検出するための良い正規表現パターンを掲載していますhttp://daringfireball.net/2010/07/improved_regex_for_matching_urls

+0

私はここで正確な正規表現のリンクを投稿しました。 – extremeboredom

+0

NSDataDetectorはまさに私が探していたものでした。私は10行のコードが好きなようにアルゴリズムをダウンさせることができました。それが存在していたことさえ知りませんでした。 – joshholat

1

フォローアップと同じように、ここで何私ですコードは次のように変更されました:

statusText.text = @"http://google.com http://www.apple.com www.joshholat.com hey there google.com"; 

NSError *error = NULL; 
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error]; 

NSArray *matches = [detector matchesInString:statusText.text 
            options:0 
             range:NSMakeRange(0, statusText.text.length)]; 

for (NSTextCheckingResult *match in matches) { 
    if ([match resultType] == NSTextCheckingTypeLink) { 
     NSLog(@"URL: %@", [[match URL] absoluteURL]); 
    } 
} 
関連する問題