以下正则表达式匹配什么?

时间:2012-12-07 14:51:47

标签: regex

我正在尝试使用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>

1 个答案:

答案 0 :(得分:3)

^     # 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

锚点对于确保域内不存在任何不允许的内容非常重要。

正如Tim Pietzker所说,这些时期不需要在角色类中进行转义。

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

相关问题