正则表达式:仅在不以特定顺序结尾时才匹配 [英] Regular expression: matching only if not ending in particular sequence

查看:25
本文介绍了正则表达式:仅在不以特定顺序结尾时才匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个不以 .html

这是我想出的模式:

[/\w\.-]+[^\.html$]

以下匹配,因为它不以 .html

The following matches because it does not end in .html

/blog/category/subcategory/

这不匹配,因为它以 .html 结尾:

This doesn't match because it ends in .html:

/blog/category/subcategory/index.html

但是,以下不匹配,虽然我希望它匹配,因为它以 .ht 而不是 .html

However, the following does not match, although I want it to match, because it ends in .ht and not .html

/blog/category/subcategory/index.ht

我应该如何改变我的模式?

How should I change my pattern?

推荐答案

如果您的正则表达式引擎支持,您可以使用否定的后视断言:

You can use a negative lookbehind assertion if your regular expression engine supports it:

^[/\w\.-]+(?<!\.html)$

如果你没有lookbehind断言但你有lookaheads,那么你可以改用它:

If you don't have lookbehind assertions but you do have lookaheads then you can use that instead:

^(?!.*\.html$)[/\w\.-]+$

在线查看:rubular

这篇关于正则表达式:仅在不以特定顺序结尾时才匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆