iPhone - Reg为url有效性 [英] iPhone - Reg Exp for url validity

查看:177
本文介绍了iPhone - Reg为url有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个聊天视图,用户可以在其中发送网址。
如果是网址,我想让用户按下链接并打开网页视图。

I have a chat view, where users can send urls to one another. In case of a url, I want to let the user press on the link and open a web view.

我正在使用使用RegexKitLite的IFTweetLabel。
目前唯一可用的支持是url以http / https开头。
我想支持没有http的链接,例如:www.nytimes.com,甚至没有www,nytimes.com。 (和其他一些扩展)。

I'm using IFTweetLabel which uses RegexKitLite. Currently the only support available is if the url starts with http/https. I want to support links without the http, for example : www.nytimes.com , and even without the "www" , nytimes.com. (and bunch of other extentions).

这是http / s前缀reg exp:

This is the http/s prefix reg exp :

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

有人可以告诉我其他正则表达式我需要回答我的其他要求。

Can someone tell me the other regular expressions I need to answer my other requirements.

我尝试使用这个,但将其添加到目标c代码会产生很多问题。

I tried using This one, but adding it to objective c code generates a lot of issues.

谢谢

推荐答案

这将匹配 http://example.org 和www.example.org。

This will match both http://example.org and www.example.org.

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

虽然我添加了一个匹配组,但请检查RegExp返回的匹配/搜索结果,以便在正确的位置重新插入正确的参数。

Although i added a "match group", so check the match/search result returned by the RegExp so the right parameters are re-inserted in the right place.

如果你可以发布整个代码sn ippet,它会更容易。

If you could post the entire code snippet, it would be easier.

RegExp说明:

(
    (
        [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"
)

这篇关于iPhone - Reg为url有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆