2016-02-09 49 views
1

我試圖創建一個PCRE正則表達式來匹配列表中除了一個以外的列表中的每個文件。當然,以編程方式進行這種排除會是一個笑話,但我必須用正則表達式來做,因爲我別無選擇。這是我創建至今:正則表達式 - Negative Lookahead

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

基本上,我需要用PHP擴展extept transport.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,但也包含了擴展前的其他字符),這是不正常的我。但我不知道如何解決這個問題。任何建議?

+1

開始你只需要確認字面'.'是在向前看運輸後不匹配。 –

回答

3

使用該負前瞻:

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

RegEx Demo

(?!transport),另一方面將無法匹配任何文件,transport