iPhone - URL 有效性的 Reg Exp
我有一个聊天视图,用户可以在其中互相发送网址。 如果是 url,我想让用户按下链接并打开网页视图。
我正在使用 IFTweetLabel,它使用 RegexKitLite。 目前唯一可用的支持是 url 以 http/https 开头。 我想支持没有http的链接,例如:www.nytimes.com,甚至没有“www”,nytimes.com。 (以及一堆其他扩展)。
这是 http/s 前缀 reg exp :
@"([hH][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])
有人可以告诉我我需要回答我的其他要求的其他正则表达式吗?
我尝试使用这个,但是将其添加到 Objective C 代码中会产生很多问题。
谢谢
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.
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).
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.
I tried using This one, but adding it to objective c code generates a lot of issues.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是 John Grubers URL 匹配正则表达式:
以下是我通过混合我拥有的其他一些正则表达式而提出的正则表达式周围和一大块 Grubers 正则表达式:
以下是一个示例程序,通过 RegexKitLite 演示了每个正则表达式与示例文本的匹配内容:
代码:
编译为:
运行时:
编辑 2011/05/27: 对正则表达式进行了细微更改,以修复与
(
)
括号正确。编辑2011/05/27:发现上面的正则表达式不能很好地处理一些额外的极端情况。更新了正则表达式:
...作为 Obj-C 字符串:
OP 还询问如何确保尾随 TLD 是“有效”。这是相同的正则表达式,采用 Obj-C 字符串形式,包含所有当前有效的 TLD(截至2011年5月27日):
The following is John Grubers URL Matching Regex:
The following is a regex I came up with by blending a few other regexes I had around and a good chunk of Grubers regex:
The following is a sample program that demonstrates, via RegexKitLite, what each regex matches against the sample text of:
The code:
Compile with:
When run:
EDIT 2011/05/27: Made a minor change to the regex to fix a problem where it wasn't matching
(
)
parenthesis correctly.EDIT 2011/05/27: Found some additional corner cases that the regex above didn't handle well. Updated regex:
... as an Obj-C string:
The OP also asked for how to make sure the trailing TLD was "valid". Here's the same regex, in Obj-C string form, with all the the currently valid TLDs (as of 2011/05/27):
这将匹配 http://example.org 和 www.example.org。
尽管我添加了“匹配组”,但请检查 RegExp 返回的匹配/搜索结果,以便将正确的参数重新插入到正确的位置。
如果您可以发布整个代码片段,那就更容易了。
正则表达式解释:
This will match both http://example.org and www.example.org.
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.
If you could post the entire code snippet, it would be easier.
RegExp explanation:
您不想为此使用正则表达式。
您需要一个
NSDataDetector
,它会为您找到所有内容。You don't want to use a regular expression for this.
You want an
NSDataDetector
, and it'll find them all for you.