我在网站上找到了这个正则表达式.它被认为是最好的URL验证表达,我同意.Diego Perini创造了它.
我面临的问题是尝试使用它objective-C来检测字符串上的URL.我一直在使用类似的选项尝试NSRegularExpressionAnchorsMatchLines,NSRegularExpressionIgnoreMetacharacters和其他人,但仍没有运气.
表达式格式不正确Objective-C吗?我错过了什么吗?有任何想法吗?
我也试过了John Gruber的正则表达式,但它失败了一些无效的URL.
Regular Expression Explanation of expression
^ match at the beginning
//Protocol identifier
(?:
(?:https?|ftp http, https or ftp
):\\/\\/ ://
)? optional
// User:Pass authentication
(?:
^\\s+ non white spaces, 1 or more times
(?:
:^\\s* : non white spaces, 0 or more times, optionally
)?@ @
)? optional
//Private IP Addresses ?! Means DO NOT MATCH ahead. So do not match any of the following …Run Code Online (Sandbox Code Playgroud) 在创建tableViewCell的内容视图时,我在drawRect中使用了drawInRect:withFont,drawAtPoint ......等等,因为我需要的只是放置一些文本.事实证明,所绘制文本的一部分需要是可点击的URL.所以我决定创建一个UIWebView并将其插入到我的drawRect中.一切似乎都很好,问题是没有发生与UIWebView的交互.我尝试启用交互,没有用.我想知道,因为使用drawRect:我正在绘制当前的图形上下文,我可以做些什么来让我的子视图与用户交互?
这是我在UITableViewCell类中使用的一些代码.
-(void) drawRect:(CGRect)rect{
NSString *comment = @"something";
[comment drawAtPoint:point forWidth:195 withFont:someFont lineBreakMode:UILineBreakModeMiddleTruncation];
NSString *anotherCommentWithURL = @"this comment has a URL http://twtr.us";
UIWebView *webView = [[UIWebView alloc] initWithFrame:someFrame];
webView.delegate = self;
webView.text = anotherCommentWithURL;
[self addSubView:webView];
[webView release];
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,视图很好,但是没有与外部的webView交互.URL嵌入到HTML中,应该是可点击的.它不是.我让它在另一个视图上工作,但在那个视图上我提出了看法.有任何想法吗?