正则表达式特定字符串的任何大写变体

时间:2015-08-13 23:56:41

标签: regex iis url-rewriting

我试图捕获特定字符串thefolder是否包含任何大写字符。

http://www.example.com/thefolder = false
http://www.example.com/theDirectory = false
http://www.example.com/theFolder = true
http://www.example.com/ThefolDeR = true
http://www.example.com/THEFOLDER = true

到目前为止,我有以下正则表达式,但它也会返回theDirectory的真实值,这是不可取的。

(?=.*[A-Z]).+

重要的是要注意它必须与特定字符串thefolder完全匹配,因为这是IIS重写,不得影响网站的其余部分。

3 个答案:

答案 0 :(得分:2)

这可能有用。使用前瞻来查看thefolder(长度9)是否有一个 大写字母。如果是,则匹配。

(?=(?i:thefolder))(?![a-z]{9})(.{9})

Formatted:

 (?=
      (?i: thefolder )
 )
 (?! [a-z]{9} )
 ( .{9} )

在最坏的情况下,总是存在排列(没有语法,但是类和交替) 但是,这总会导致一个大字符串。

T[hH][eE][fF][oO][lL][dD][eE][rR]|[tT]H[eE][fF][oO][lL][dD][eE][rR]|[tT][hH]E[fF][oO][lL][dD][eE][rR]|[tT][hH][eE]F[oO][lL][dD][eE][rR]|[tT][hH][eE][fF]O[lL][dD][eE][rR]|[tT][hH][eE][fF][oO]L[dD][eE][rR]|[tT][hH][eE][fF][oO][lL]D[eE][rR]|[tT][hH][eE][fF][oO][lL][dD]E[rR]|[tT][hH][eE][fF][oO][lL][dD][eE]R

   T [hH] [eE] [fF] [oO] [lL] [dD] [eE] [rR] 
|  [tT] H [eE] [fF] [oO] [lL] [dD] [eE] [rR] 
|  [tT] [hH] E [fF] [oO] [lL] [dD] [eE] [rR] 
|  [tT] [hH] [eE] F [oO] [lL] [dD] [eE] [rR] 
|  [tT] [hH] [eE] [fF] O [lL] [dD] [eE] [rR] 
|  [tT] [hH] [eE] [fF] [oO] L [dD] [eE] [rR] 
|  [tT] [hH] [eE] [fF] [oO] [lL] D [eE] [rR] 
|  [tT] [hH] [eE] [fF] [oO] [lL] [dD] E [rR] 
|  [tT] [hH] [eE] [fF] [oO] [lL] [dD] [eE] R

答案 1 :(得分:1)

如果您只是进行IIS重写,则可以匹配thefolder并选中忽略大小写复选框,然后执行您需要执行的操作。

在重写配置文件中尝试这样的事情:

<rule name="LowerCaseRule1" stopProcessing="true">
   <match url="thefolder" ignoreCase="true" />
   <action type="Rewrite" url="{ToLower:{URL}}" />
</rule>

<强>更新

这有点令人费解,但这个正则表达式应该这样做。

.*(?=.*[A-Z])([tT][hH][eE][fF][oO][lL][dD][eE][rR]).*

答案 2 :(得分:-1)

我认为这个正则表达式是您正在寻找的:

^[^A-Z]+$

Here就是一个例子。

返回:

http://www.example.com/thefolder    = true
http://www.example.com/theDirectory = false
http://www.example.com/theFolder    = false
http://www.example.com/ThefolDeR    = false
http://www.example.com/THEFOLDER    = false

如果你想选择完全相反的方法,你可以这样做:

^.*[A-Z].*$

Here 就是一个例子。

返回:

http://www.example.com/thefolder    = false
http://www.example.com/theDirectory = true
http://www.example.com/theFolder    = true
http://www.example.com/ThefolDeR    = true
http://www.example.com/THEFOLDER    = true