2011-05-26 55 views
2

我有一個聊天視圖,用戶可以發送URL到彼此。 如果有網址,我想讓用戶按鏈接並打開網頁視圖。iPhone - 註冊精通的URL有效性

我使用它使用RegexKitLite IFTweetLabel。 目前唯一可用的支持是如果URL以http/https開頭。 我想支持鏈接沒有http,例如:www.nytimes.com,甚至沒有「WWW」,nytimes.com。 (和其他一些延伸)。

這是HTTP/S前綴REG EXP:

@"([hH][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)]) 

誰能告訴我其他的正則表達式我需要回答我的其他要求。

我試着用This之一,但它添加到目標C代碼生成了大量的問題。

感謝

回答

3

這將同時匹配http://example.org和www.example.org。

@"(([hH][tT][tT][pP][sS]?:\\/\\/|www\\.)[^ ,'\">\\]\\)]*\\.[^\\. ,'\">\\]\\)]{2,6}) 

雖然我添加了一個「匹配組」,所以檢查所以正確的參數被重新插入在正確的位置由所述正則表達式返回的匹配/搜索結果。

如果你能張貼整個代碼段,它會更容易。

正則表達式的解釋:

(
    (
     [hH][tT][tT][pP][sS]?:\/\/ # Match HTTP/http (and hTtP :) 
     |        # OR 
     www\.       # www<literal DOT> 
    ) 
    [^ ,'\">\]\)]*     # Match at least 1 character that are not any of space, comma, apostrophe, quotation mark, "more than", "right square bracket", "right parenthese" 
    \.        # Match <literal DOT> 
    [^\. ,'\">\]\)]{2,6}    # Match 2-6 characters that are not any of dot, space, comma, apostrophe, quotation mark, "more than", "right square bracket", "right parenthese" 
) 
+0

真棒,現在怎麼樣沒有「www」的example.org呢? – Idan 2011-05-26 12:00:05

+0

另一件事,在你提供的reg exp中,像「www.example」這樣的字符串仍然可以工作,即使它並不真正有效。我不想那樣做。你可以將這兩個要求都添加到reg exp中嗎?謝謝! – Idan 2011-05-26 12:23:59

+0

這將是非常複雜的。如果你時間不夠,我建議你去http://www.regular-expressions.info/tutorial.html。這樣你可以使RegExp完全適合你的需求。 – joar 2011-05-26 12:40:55

6

以下爲John Grubers URL Matching Regex

(?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`!()\[\]{};:'".,<>?«»「」‘’]) 

下面是我通過配合一些其他的正則表達式我周圍和良好的大塊想出了一個正則表達式Grubers正則表達式:

(?i)\b(?:(?:[a-z][\w\-]+://(?:\S+?(?::\S+?)?\@)?)|(?:(?:[a-z0-9\-]+\.)+[a-z]{2,4}))(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]*\)))*\))*(?<![\s`!()\[\]{};:'".,<>?«»「」‘’]) 

以下是演示了一個示例程序,通過RegexKitLite,什麼每個正則表達式對的樣本文本匹配:

你看到 http://www.stackoverflow.com?或者 http://www.stackoverflow.com/

然後有 www.stackoverflow.com/,以及 www.stackoverflow.com/index。

也許像stackoverflow.com 額外stackoverflow.com?或者 「stackoverflow.com」?

也許jobs.stackoverflow.com或 'http://twitter.com/#!/CHOCKENBERRY', CHOCKLOCK !!

文件 @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?

也許 http://www.yahoo.com/index///i.htmlhttp://www.yahoo.com/////xyz.html?!

代碼:

#import <Foundation/Foundation.h> 
#import "RegexKitLite.h" 

int main(int argc, char *argv[]) { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    NSString *urlRegex = @"(?i)\\b(?:(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)|(?:(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}))(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*(?<![\\s`!()\\[\\]{};:'\".,<>?«»「」‘’])"; 

    // John Gruber's URL matching regex from http://daringfireball.net/2010/07/improved_regex_for_matching_urls 
    NSString *gruberURLRegex = @"(?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`!()\\[\\]{};:'\".,<>?«»「」‘’])"; 

    NSString *urlString = @"Did you see http://www.stackoverflow.com? Or http://www.stackoverflow.com/?\n\nAnd then there is www.stackoverflow.com/, along with www.stackoverflow.com/index.\n\nMaybe something like stackoverflow.com with extra stackoverflow.com? Or \"stackoverflow.com\"?\n\nPerhaps jobs.stackoverflow.com, or 'http://twitter.com/#!/CHOCKENBERRY', the CHOCKLOCK!!\n\nFile @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook?\n\nMaybe http://www.yahoo.com/index///i.html! http://www.yahoo.com/////xyz.html?!"; 

    NSLog(@"String :\n\n%@\n\n", urlString); 

    NSLog(@"Matches: %@\n", [urlString componentsMatchedByRegex:urlRegex]); 

    NSLog(@"Gruber URL Regex Matches: %@\n", [urlString componentsMatchedByRegex:gruberURLRegex]); 

    [pool release]; pool = NULL; 
    return(0); 
} 

編譯:

shell% gcc -o url url.m RegexKitLite.m -framework Foundation -licucore 

當運行:

shell% ./url 
2011-05-27 20:32:58.204 url[25520:903] String : 

Did you see http://www.stackoverflow.com? Or http://www.stackoverflow.com/? 

And then there is www.stackoverflow.com/, along with www.stackoverflow.com/index. 

Maybe something like stackoverflow.com with extra stackoverflow.com? Or "stackoverflow.com"? 

Perhaps jobs.stackoverflow.com, or 'http://twitter.com/#!/CHOCKENBERRY', the CHOCKLOCK!! 

File @file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook? 

Maybe http://www.yahoo.com/index///i.html! http://www.yahoo.com/////xyz.html?! 

2011-05-27 20:32:58.211 url[25520:903] Matches: (
    "http://www.stackoverflow.com", 
    "http://www.stackoverflow.com/", 
    "www.stackoverflow.com/", 
    "www.stackoverflow.com/index", 
    "stackoverflow.com", 
    "stackoverflow.com", 
    "stackoverflow.com", 
    "jobs.stackoverflow.com", 
    "http://twitter.com/#!/CHOCKENBERRY", 
    "file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook", 
    "http://www.yahoo.com/index///i.html", 
    "http://www.yahoo.com/////xyz.html" 
) 
2011-05-27 20:32:58.213 url[25520:903] Gruber URL Regex Matches: (
    "http://www.stackoverflow.com", 
    "http://www.stackoverflow.com/", 
    "www.stackoverflow.com/", 
    "www.stackoverflow.com/index", 
    "http://twitter.com/#!/CHOCKENBERRY", 
    "file:///Users/johne/rkl/rkl.html#RegexKitLiteCookbook", 
    "http://www.yahoo.com/index///i.html", 
    "http://www.yahoo.com/////xyz.html" 
) 

編輯2011年5月27日:做了一個小的改動正則表達式來解決它不是馬的問題正確地加上()括號。

編輯2011/05/27:發現上面的正則表達式處理不好的一些額外的角落案例。更新的正則表達式:

(?i)\b(?:[a-z][\w\-]+://(?:\S+?(?::\S+?)?\@)?)?(?:(?:(?<!:/|\.)(?:(?:[a-z0-9\-]+\.)+[a-z]{2,4}(?![a-z]))|(?<=://)/))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]*\)))*\))*)(?<![\s`!()\[\]{};:'".,<>?«»「」‘’]) 

...作爲一個對象 - 字符串:

@"(?i)\\b(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)?(?:(?:(?<!:/|\\.)(?:(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}(?![a-z]))|(?<=://)/))(?:(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*)(?<![\\s`!()\\[\\]{};:'\".,<>?«»「」‘’])"; 

的OP也問如何確保尾隨TLD是「有效的」。這是同樣的正則表達式中的OBJ-C字符串形式,所有的currently valid TLDs(如2011年5月27日的):

@"(?i)\\b(?:[a-z][\\w\\-]+://(?:\\S+?(?::\\S+?)?\\@)?)?(?:(?:(?<!:/|\\.)(?:(?:[a-z0-9\\-]+\\.)+(?:(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--3e0b707e|xn--45brj9c|xn--80akhbyknj4f|xn--90a3ac|xn--9t4b11yi5a|xn--clchc0ea0b2g2a9gcd|xn--deba0ad|xn--fiqs8s|xn--fiqz9s|xn--fpcrj9c3d|xn--fzc2c9e2c|xn--g6w251d|xn--gecrj9c|xn--h2brj9c|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--j6w193g|xn--jxalpdlp|xn--kgbechtv|xn--kprw13d|xn--kpry57d|xn--lgbbat1ad8j|xn--mgbaam7a8h|xn--mgbayh7gpa|xn--mgbbh1a71e|xn--mgbc0a9azcg|xn--mgberp4a5d4ar|xn--o3cw4h|xn--ogbpf8fl|xn--p1ai|xn--pgbs0dh|xn--s9brj9c|xn--wgbh1c|xn--wgbl6a|xn--xkc2al3hye2a|xn--xkc2dl3a5ee0h|xn--yfro4i67o|xn--ygbi2ammx|xn--zckzah|xxx|ye|yt|za|zm|zw))(?![a-z]))|(?<=://)/))(?:(?:[^\\s()<>]+|\\((?:[^\\s()<>]+|(?:\\([^\\s()<>]*\\)))*\\))*)(?<![\\s`!()\\[\\]{};:'\".,<>?«»「」‘’])"; 
+0

哇,真棒回答,非常感謝! – Idan 2011-05-29 07:32:05

+0

這似乎沒有拒絕無效的計劃,如'htp://' – 2013-01-25 07:30:19

3

你不想使用正則表達式這一點。

你想要一個NSDataDetector,它會爲你找到它們。

+0

謝謝,沒有意識到這一點。 – Idan 2011-05-29 07:32:59