正确匹配 IDN URL

发布于 2024-08-16 06:30:05 字数 1081 浏览 1 评论 0原文

我需要帮助构建一个可以正确匹配自由文本中的 URL 的正则表达式。

  • 方案
    • 以下其中一项:ftphttphttpsftps 是一种协议吗?)
  • 可选用户(和可选的密码
  • 主机(支持 IDN)
    • 支持www子域(支持 IDN)
    • TLD 的基本过滤(我认为[a-zA-Z]{2,6} 就足够了)
  • 可选端口编号
  • 路径(可选,支持 Unicode 字符)
  • 查询(可选,支持 Unicode 字符)
  • 片段(可选,支持 Unicode 字符)

以下是我可以找到的有关子域的信息:

“子域”表示相对 依赖,而不是绝对依赖: 例如,wikipedia.org 包括 org 域的子域,以及 en.wikipedia.org 包含一个子域 域名 wikipedia.org。在 理论上,这个细分可以下降 深度达 127 级,每个 DNS 标签 最多可以包含 63 个字符,如 只要整个域名不 总长度超过255 字符。

关于域名本身,我找不到任何可靠的来源,但我认为非 IDN 的正则表达式(我不知道如何编写 IDN 兼容版本)类似于:

[0-9a-zA-Z][0-9a-zA-Z\-]{2,62}

>有人可以帮我解决这个正则表达式或给我指出一个好的方向吗?

I need help building a regular expression that can properly match an URL inside free text.

  • scheme
    • One of the following: ftp, http, https (is ftps a protocol?)
  • optional user (and optional pass)
  • host (with support for IDNs)
    • support for www and sub-domain(s) (with support for IDNs)
    • basic filtering of TLDs ([a-zA-Z]{2,6} is enough I think)
  • optional port number
  • path (optional, with support for Unicode chars)
  • query (optional, with support for Unicode chars)
  • fragment (optional, with support for Unicode chars)

Here is what I could find out about sub-domains:

A "subdomain" expresses relative
dependence, not absolute dependence:
for example, wikipedia.org comprises a
subdomain of the org domain, and
en.wikipedia.org comprises a subdomain
of the domain wikipedia.org. In
theory, this subdivision can go down
to 127 levels deep, and each DNS label
can contain up to 63 characters, as
long as the whole domain name does not
exceed a total length of 255
characters.

Regarding the domain name itself I couldn't find any reliable source but I think the regular expression for non-IDNs (I'm not sure how to write a IDN compatible version) is something like:

[0-9a-zA-Z][0-9a-zA-Z\-]{2,62}

Can someone help me out with this regular expression or point me to a good direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵情客 2024-08-23 06:30:05

John Gruber 因《大胆火球》而闻名,最近发表了一篇文章,详细介绍了他对良好 URL 的追求- 识别正则表达式字符串。他想出的是这样的:

\b(([\w-]+://?|www[.])[^\s()>>]+(?:\([\ w\d]+\)|([^[:punct:]\s]|/)))

这显然也适用于包含 Unicode 的 URL。您需要对其进行轻微修改才能获得您要查找的其余内容 - 方案、用户名、密码等。 Alan Storm 写了一篇解释 Gruber 的正则表达式模式的文章,我绝对需要它(正则表达式是如此编写一次就没有线索如何再次阅读!)。

John Gruber, of Daring Fireball fame, had a post recently that detailed his quest for a good URL-recognizing regex string. What he came up with was this:

\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))

Which apparently does OK with Unicode-containing URLs, as well. You'd need to do the slight modification to it to get the rest of what you're looking for -- the scheme, username, password, etc. Alan Storm wrote a piece explaining Gruber's regex pattern, which I definitely needed (regex is so write-once-have-no-clue-how-to-read-ever-again!).

折戟 2024-08-23 06:30:05

如果您需要协议并且不太担心误报,到目前为止最简单的事情就是匹配 :// 周围的所有非空白字符

If you require the protocol and aren't worried too much about false positives, by far the easiest thing to do is match all non-whitespace characters around ://

花辞树 2024-08-23 06:30:05

这将帮助您完成大部分工作。如果您需要更精细的请提供测试数据。

(ftp|https?)://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?

This will get you most of the way there. If you need it more refined please provide test data.

(ftp|https?)://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文