2017-08-11 36 views
-3

我想通過使用正則表達式從文件名中沒有「test」和「dummy」的位置讀取僅文件。如何使用Python使用正則表達式搜索字符串中的多個單詞

例如文件路徑下面有文件。

file1.csv 
file2.txt 
20170811_test_1.dat 
1_dummy_20170811.dat 

在這裏,我需要找到file1.csv和FILE2.TXT並排除其中有測試或虛所有其他文件(可以是任何情況下/上的)。

+5

所以,Python或C#? –

+0

檢查此鏈接 - https://superuser.com/questions/903168/how-should-i-write-a-regex-to-match-a-specific-word – AGrammerPro

+0

通常有核心的方法,讓你做基本搜索子字符串。在Python中,您可以執行[str.find()](https://docs.python.org/2/library/string.html#string.find) – linden2015

回答

2

你可以試試這個:

^(?!.*test|.*dummy).*$ 

^:聲明線

的開頭?! :不包含測試OR(|)假,甚至沒有在開始時

*(*):比賽中的所有字符

$:宣佈該行的末尾

相關問題