正则表达式:仅当不以特定序列结尾时才匹配

Regular expression: matching only if not ending in particular sequence

提问人:Kevin Le - Khnle 提问时间:2/12/2011 更新时间:2/12/2011 访问量:31343

问:

我想测试一个不以 .html 结尾的网址

这是我想出的模式:

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

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

/blog/category/subcategory/

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

/blog/category/subcategory/index.html

但是,以下内容不匹配,尽管我希望它匹配,因为它以 .ht 而不是 .html 结尾

/blog/category/subcategory/index.ht

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

正则表达式

评论

1赞 Mark Byers 2/12/2011
您使用的是什么编程语言或工具?

答:

18赞 Lily Ballard 2/12/2011 #1

你用的是什么引擎?如果它支持前瞻断言,则可以执行以下操作:

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

如果我们将其分解为组件,它看起来像这样:

(            # start a group for the purposes of repeating
 (?!\.html$) # negative lookahead assertion for the pattern /\.html$/
 [/\w.-]     # your own pattern for matching a URL character
)+           # repeat the group

这意味着,对于每个字符,它会在使用字符之前测试模式 /.html$/ 在此处无法匹配。

您可能还希望在开头和结尾锚定整个模式,以强制它与整个 URL 匹配 - 否则只能匹配 URL 的一部分。随着这种变化,它变成了^$

/^((?!\.html$)[/\w.-])+$/
36赞 Mark Byers 2/12/2011 #2

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

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

如果您没有 lookbehind 断言,但您有 lookahead,则可以改用它:

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

在线查看它:rubular

评论

0赞 Mark Byers 2/12/2011
@Khnle:我预计这取决于引擎和输入字符串的分布。