2014-03-26 50 views
0

我有這樣的URL正則表達式的方法;URL正則表達式崩潰的iOS應用程序

NSString *urlRegEx = @"(http://|https://|www.)((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*)|([#^?'+!&%[email protected]=*]))+((\\w)*|([0-9]*|([#^?'+!&%[email protected]=*])))+"; 
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
NSArray *urlArray = [textView.text componentsSeparatedByString:@" "]; 

它工作的很好,但是如果URL包含' - '或'_'應用程序崩潰。你對問題有什麼想法嗎?

+0

測試用例? iOS版本?你是否在不同的iOS版本上測試過它? – DarkDust

+0

它在哪裏崩潰?在評估謂詞或稍後嘗試使用它的不同部分時? –

+0

沒有人我的urlArray崩潰原因textView.text謂詞不適用於我的urlRegex。我找到解決方案。 –

回答

0

好夥計我找到了這樣的解決方案;

NSString *urlRegEx = @"(http://|https://|www.)((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*)|([-_#^?'+!&%[email protected]=*]))+((\\w)*|([0-9]*|([-_#^?'+!&%[email protected]=*])))+";