3

我需要匹配一个主机名——但不想要 tld:

example.com =~ /regex/ => 示例

sub.example.com =~ /regex/ => sub.example

sub.sub.example.com =~ /regex/ => sub.sub.example

对正则表达式有任何帮助吗?谢谢。

4

6 回答 6

4

Assuming your string is correctly formatted and doesn't include things like protocol [i.e. http://], you need all characters up to but not including the final .tld.

So this is the simplest way to do this. The trick with regular expressions is not to overcomplicate things:

.*(?=\.\w+)

This basically says, give me all characters in the set that is followed by [for example] .xxx, which will basically just return everything prior to the last period.

If you don't have lookahead, it would probably be easiest to use:

(\w+\.)+

which will give you everything up to and including the final '.' and then just trim the '.'.

于 2009-05-07T19:40:04.390 回答
1

尝试这个

/.+(?=\.\w+$)/

如果没有 ?= 的支持,它将是

/(.+)\.\w+$/

然后取第一组的内容

于 2009-05-07T19:40:53.827 回答
-1

You could just strip off the tld:

s/\.[^\.]*$//;
于 2009-05-07T19:36:02.637 回答
-2
(?<Domain>.*)\.(?<TLD>.*?)$
于 2009-05-07T19:33:54.797 回答
-2
(.*)\.

这并不是真正特定于 tlds,它只会为您提供一行中最后一个句点之前的所有内容。如果您想对有效的 TLD 或任何内容严格,则必须以不同的方式编写。

于 2009-05-07T19:31:44.847 回答
-3

我不清楚你想让比赛如何进行。但是使用通常的扩展正则表达式,您应该能够将任何 tld 与[a-zA-Z]{2,3}所以如果您尝试获取 tld 以外的整个名称,例如

\(.\)\.[a-zA-Z]{2,3}$

应该很近。

于 2009-05-07T19:32:16.173 回答