我有一個包含大量文件的目錄。文件名與以下類似:(數字)一(數字),其中(數字)可以是任何數字。還有一些名稱爲:(號碼)的文件,其中(號碼)可以是任何數字。我想知道如何在文件名末尾添加額外的「一個(數字)」文件數。如何在python中執行string.startswith()時忽略數字?
比方說,我有文件名列表,我想這樣做
for n in list:
if n.startswith(the(number)one):
add one to a counter
的有反正它在(數)的空間做了startswith時接受任何數目?
例子: the34one5 the37one2 the444one3 the87one8 the34 the32
這應返回4.
我認爲正則表達式應該是「one \ d + $」,因爲OP指定他想在文件名的末尾匹配__「one(number)」或者一個完整的正則表達式「the \ d + one \ d +」並使用'match()'而不是'search()'。 – mouad 2011-06-10 15:59:07
你是對的,我添加了第二個更精確匹配的例子。 – 2011-06-10 16:06:24