简体   繁体   English

具有特定取消资格的正则表达式匹配模式

[英]Regex match pattern with specific disqualifier

I have a regex that will remove .php from the end of a URL:我有一个正则表达式,可以从 URL 的末尾删除 .php:

^(.*)\\.php$

this will match www.mysite.com/something.php with the first capturing group being www.mysite.com/something这将匹配 www.mysite.com/something.php 与第一个捕获组是 www.mysite.com/something

I want to tweak this to match all urls like this a excluding specific url path.我想调整它以匹配所有这样的 url 排除特定的 url 路径。 For instance "exclude.php" should not be matched and thus leave the .php例如“exclude.php”不应该被匹配,因此离开.php

www.mysite.com/exclude.php -> www.mysite.com/exclude.php www.mysite.com/exclude.php -> www.mysite.com/exclude.php

but for any other path it should remove it:但对于任何其他路径,它应该将其删除:

www.mysite.com/anythingelse.php -> www.mysite.com/anythingelse www.mysite.com/anythingelse.php -> www.mysite.com/anythingelse

You can use a negative lookbehind for excluding a text before .php :您可以使用否定回溯来排除.php之前的文本:

^(.+)(?<!\/exclude)\.php$

RegEx Demo正则表达式演示

(?<!\\/exclude) is negative lookbehind to assert failure if .php is preceded by /exclude (?<!\\/exclude)如果.php前面有/exclude则为否定后视断言失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM