正则表达式 - 负前瞻

Regex - Negative Lookahead

我正在尝试创建一个 PCRE 正则表达式来匹配列表中除一个文件之外的每个文件。当然,以编程方式执行此排除是一个笑话,但我必须使用正则表达式来执行此操作,因为我别无选择。这是我到目前为止创建的:

^(?!transport).*\.php$

基本上,我需要匹配扩展名为 transport.php 的每个文件 php。所以这是我的测试结果:

transport.php           # Not Matched! Good!
test_transport.php      # Matched! Good!
transport_test.php      # Not Matched! Bad!
test_transport_test.php # Matched! Good!

好吧,问题是我的正则表达式不匹配像 transport(.*)\.php 这样的文件(以 transport 开头,但在扩展名之前还包含其他字符),这对我来说不合适。但我不知道如何解决这个问题。有什么建议吗?

使用这个负前瞻:

^(?!transport\.php$).*\.php$

RegEx Demo

另一方面,

(?!transport) 将无法匹配任何以 transport

开头的文件