为什么这个正则表达式前瞻不起作用?

我正在设计一个在一些IIS Url重写中使用的正则表达式.目的是捕获以下网址:

>不仅仅是根目录中的文件(通过包含句点标识),以及
>不要包含查询字符串,并且
>不属于特定的子目录集,特别是“帐户”和“公共”

我目前的正则表达式如下:

^(?!(Account)|(Public))([^./]+)(/[^?]*)?$

使用RegexPal测试集:

file.aspx
Account/otherfile.aspx
Public/otherfile.aspx
otherfolder1/otherfile.aspx?stuff=otherstuff
otherfolder2/otherfolder/otherfile.aspx
otherfolder3/
otherfolder4

我的正则表达式正确地忽略了前两种情况,但它仍然匹配第三种情况.前瞻有什么问题?

解决方法

我无法抗拒试图提出一些可以在RegExPal中工作的东西(没有成功 – 编辑:刚刚验证过,这确实在RegExPal中有效)但是我想我会把它作为另一种方式来做你需要的东西,这可能更容易理解:

^(?!Account|Public|[a-zA-Z_0-9]+\.)[a-zA-Z_0-9/.]+$

解释:

^                   # start
(?!                 # open a negative lookahead
Account|Public|     # ignore both Account and Public
[a-zA-Z_0-9]+\.     # ignore files in root (i.e.,letters/numbers,followed by period)
)                   # close negative lookahead
[a-zA-Z_0-9/.]+     # Now match anything with letters/numbers,periods and slashes,but no '?' (ignores URLs with query string)
$                  # end

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...