加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:50:04 所属栏目:百科 来源:网络整理
导读:我正在设计一个在一些IIS Url重写中使用的正则表达式.目的是捕获以下网址: 不仅仅是根目录中的文件(通过包含句点标识),以及 不要包含查询字符串,并且 不属于特定的子目录集,特别是“帐户”和“公共” 我目前的正则表达式如下: ^(?!(Account)|(Public))([^.
我正在设计一个在一些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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读