正则表达式匹配这个还是那个? [英] Regex match this OR that?
问题描述
正则表达式是什么?
匹配字符串
- 否(以
.php
结尾或以.html
结尾或包含/
)
- NOT ( ends in
.php
OR ends in.html
OR contains/
)
谢谢!
我需要NOT
部分,因为该表达式将在Apache mod重写中使用,因为我无法更改mod_rewrite
的逻辑来避免NOT
.
I need the NOT
part because the expression is to be used on an Apache mod rewrite, since I can't change the logic of mod_rewrite
to avoid the NOT
.
我最初的努力是([^/]+)(\.html)$(^\.php)$
-这是非常错误的
My initial effort was ([^/]+)(\.html)$(^\.php)$
- which is monstrously wrong
推荐答案
Apache mod_rewrite 确实支持否定/mod/mod_rewrite.html"rel =" noreferrer>此文档.
Apache mod_rewrite does support negation, according to this document.
在mod_rewrite中,NOT字符 ('!')也可能 模式前缀.这使您能够 否定模式可以说 实例:``如果当前URL确实存在 不符合此模式''.这可以是 用于特殊情况下 更容易匹配否定词 模式,或作为最后的默认规则.
In mod_rewrite, the NOT character ('!') is also available as a possible pattern prefix. This enables you to negate a pattern; to say, for instance: ``if the current URL does NOT match this pattern''. This can be used for exceptional cases, where it is easier to match the negative pattern, or as a last default rule.
因此,您应该可以执行以下操作:
So you should be able to do something like:
!^(.*?/.*|.*?\.(?:php|html)$)$
这应该与不包含/并且以.php或.html结尾的任何内容匹配
And that should match anything which does not contain / and does not end in .php or .html
这篇关于正则表达式匹配这个还是那个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!