Pcrepp - 用于匹配主机名的 Perl 正则表达式语法

发布于 2024-08-23 05:24:39 字数 600 浏览 4 评论 0原文

可能的重复:
主机名正则表达式

我正在尝试使用 pcrepp (PCRE) 从 url 中提取主机名。 PCRE 正则表达式与 Perl 5 正则表达式相同。

例如:

url = "http://www.pandora.com/#/volume/73";
// the match will be "http://www.pandora.com/".

我找不到此示例的正则表达式的正确语法。

  • 需要适用于任何网址:amazon.com/sds/ 应返回:amazon.com。 或 abebooks.co.uk/isbn="62345627457245"/blabla/ 应该返回 abebooks.co.uk
  • 我不需要检查网址是否有效。只是为了获取主机名。

Possible Duplicate:
The Hostname Regex

I'm trying to use pcrepp (PCRE) to extract hostname from url.
the pcre regular expression is as same as Perl 5 regular expression.

for example:

url = "http://www.pandora.com/#/volume/73";
// the match will be "http://www.pandora.com/".

I can't find the correct syntax of the regex for this example.

  • Needs to work for any url: amazon.com/sds/ should return: amazon.com.
    or abebooks.co.uk/isbn="62345627457245"/blabla/ should return abebooks.co.uk
  • I don't need to check if the url is valid. just to get the hostname.

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

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

发布评论

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

评论(4

黑寡妇 2024-08-30 05:24:40

像这样的东西:

^(?:[a-z]+://)?[^/]+/?

Something like this:

^(?:[a-z]+://)?[^/]+/?
小伙你站住 2024-08-30 05:24:40

请参阅 Regexp::Common::URI::http它使用 Regexp::Common::URI 中定义的子模式: :RFC2396。检查这些模块的源代码应该会让您了解如何组合一个合适的模式。

See Regexp::Common::URI::http which uses sub-patterns defined in Regexp::Common::URI::RFC2396. Examining the source code of those modules should give you a good idea how to put together a decent pattern.

可遇━不可求 2024-08-30 05:24:40

这是一种可能性:

^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$

另一种:

^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$

这些和其他与 URL 相关的正则表达式可以在这里找到: 正则表达式图书馆

Here is one possibility:

^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$

And another:

^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$

These and other URL related regular expressions can be found here: Regular Expression Library

宫墨修音 2024-08-30 05:24:40
string regex1, regex2, finalRegex; 
regex1 = "^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??";

regex2 = "([^#]+)?#?(\\w*)";

    //concatenation
    finalRegex= regex1+regex2;

结果将是第六名。
在我问的另一个问题中回答:详细信息

string regex1, regex2, finalRegex; 
regex1 = "^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??";

regex2 = "([^#]+)?#?(\\w*)";

    //concatenation
    finalRegex= regex1+regex2;

the result will be at the sixth place.
answered in another question I asked: Details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文