Regex 下面的正则表达式匹配什么?

Regex 下面的正则表达式匹配什么?,regex,Regex,我正在尝试使用IIS URL重写将用户带到WWW域而不是非WWW域。我遇到一篇文章,其中使用以下正则表达式匹配域名: ^[^\.]+\.[^\.]+$ 我不知道什么类型的域与这个正则表达式匹配。以下是完整的代码: <rule name="www redirect" enabled="true" stopProcessing="true"> <match url="." /> <conditions> <add input="{HTTP_

我正在尝试使用IIS URL重写将用户带到WWW域而不是非WWW域。我遇到一篇文章,其中使用以下正则表达式匹配域名:

^[^\.]+\.[^\.]+$
我不知道什么类型的域与这个正则表达式匹配。以下是完整的代码:

<rule name="www redirect" enabled="true" stopProcessing="true">
  <match url="." />
  <conditions>
    <add input="{HTTP_HOST}" **pattern="^[^\.]+\.[^\.]+$"** />
    <add input="{HTTPS}" pattern="off" />
  </conditions>
  <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="www redirect https" enabled="true" stopProcessing="true">
  <match url="." />
  <conditions>
    <add input="{HTTP_HOST}" **pattern="^[^\.]+\.[^\.]+$"** />
     <add input="{HTTPS}" pattern="on" />
    </conditions>
   <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>

锚定对于确保域周围没有不允许的内容非常重要

正如Tim Pietzker提到的,句点不需要在character类中转义


要回答你的问题,最基本的方法是:这个匹配什么?仅包含一个
,既不是第一个字符,也不是最后一个字符的任何字符串。

您可以添加,在字符类中不必转义点。最终的结果是这个正则表达式匹配一个字符串,该字符串正好包含一个点,但不以一个点开始或结束。
^     # anchor the pattern to the beginning of the string
[^\.] # negated character class: matches any character except periods
+     # one or more of those characters
\.    # matches a literal period
[^\.] # negated character class: matches any character except periods
+     # one or more of those characters
$     # anchor the pattern to the end of the string