正则表达式用于除&QUOT任何字符串; WWW"? (子) [英] Regex for ANY string except "www"? (subdomain)

查看:296
本文介绍了正则表达式用于除&QUOT任何字符串; WWW"? (子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人在那里可以帮助我在C#中的正则表达式。我认为这是相当简单的,但是为什么我有这样一个困难时期,我一直令人头大我的大脑在它并不太确切。 :)

I was wondering if someone out there could help me with a regex in C#. I think it's fairly simple but I've been wracking my brain over it and not quite sure why I'm having such a hard time. :)

我已经发现了几个例子左右,但我似乎无法操纵他们做什么,我需要。

I've found a few examples around but I can't seem to manipulate them to do what I need.

我只需要匹配任何字母数字+破折号子字符串,它是不是WWW,只是到了。

I just need to match ANY alphanumeric+dashes subdomain string that is not "www", and just up to the "."

此外,理想情况下,如果有人输入www.subdomain.domain.com我想如果可能的话被忽略了www。如果不是,这不是一个大问题。

Also, ideally, if someone were to type "www.subdomain.domain.com" I would like the www to be ignored if possible. If not, it's not a huge issue.

在换句话说,我想匹配:

In other words, I would like to match:


  • (测试) .domain.com

  • (测试2) .domain.com

  • (wwwasdf) .domain.com

  • (asdfwww) .domain.com

  • (W) .domain.com

  • (wwwwww) .domain.com

  • (ASFD-12345-WWW-香蕉) .domain.com

  • WWW。(子) .domain.com

  • (test).domain.com
  • (test2).domain.com
  • (wwwasdf).domain.com
  • (asdfwww).domain.com
  • (w).domain.com
  • (wwwwww).domain.com
  • (asfd-12345-www-bananas).domain.com
  • www.(subdomain).domain.com

和我不想要匹配:


  • (WWW) .domain.com

  • (www).domain.com

在我看来,像它应该很容易,但我有跟不匹配的一部分烦恼。

It seems to me like it should be easy, but I'm having troubles with the "not match" part.

有关它的价值,这是在IIS 7 URL重写模块使用,重写所有非www子域。

For what it's worth, this is for use in the IIS 7 URL Rewrite Module, to rewrite for all non-www subdomains.

谢谢!

推荐答案

是域名的其余部分不变,如 .domain.com ,如你的例子?试试这个:

Is the remainder of the domain name constant, like .domain.com, as in your examples? Try this:

\b(?!www\.)(\w+(?:-\w+)*)(?=\.domain\.com\b)

说明:


  • \\ w +( - ?\\ w +)* 匹配像你描述的通用域名组件(但有点更严格)

  • \w+(?:-\w+)* matches a generic domain-name component as you described (but a little more rigorously).

(?= \\域\\ .COM \\ B)确保它是第一个子域(即实际域名前的最后一个)。

(?=\.domain\.com\b) makes sure it's the first subdomain (i.e., the last one before the actual domain name).

\\ B(?!WWW \\。)确保它不是 WWW。(没有 \\ b ,它可以跳过第一个是W 和匹配在 WW。 )。

\b(?!www\.) makes sure it isn't www. (without the \b, it could skip over the first w and match just the ww.).

在我的测试中,这个正则表达式匹配precisely你在你的例子强调了零件,并执行的的匹配 WWW。中任一的最后两个实施例。

In my tests, this regex matches precisely the parts you highlighted in your examples, and does not match the www. in either of the last two examples.

编辑:这是另一个版本,全名相匹配,捕捉不同群体的作品:

Here's another version which matches the whole name, capturing the pieces in different groups:

^((?:\w+(?:-\w+)*\.)*)((?!www\.)\w+(?:-\w+)*)(\.domain\.com)$

在大多数情况下,集团 $ 1 将包含一个空字符串,因为没有什么子域名前的,但这里是它的分解 www.subdomain .domain.com

In most cases, group $1 will contain an empty string because there's nothing before the subdomain name, but here's how it breaks down www.subdomain.domain.com:

$1: "www."
$2: "subdomain"
$3: ".domain.com"

这篇关于正则表达式用于除&QUOT任何字符串; WWW"? (子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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