我有一个文件列表,我想只保留以'test_'开头并以'.py'结尾的文件。我希望正则表达式只返回'test_'和'.py'中的文本。我不想包含.pyc文件。
我试过了:
>>>filename = 'test_foo.py'
>>>re.search(r'(?<=test_).+(?=\.py)', filename).group()
foo.py
但它仍然返回扩展名,并允许'.pyc'扩展名(我不想要)。我很确定它是消耗整个字符串的'+'。
这可以作为后备,但我更喜欢正则表达式解决方案:
>>>filename = 'test_foo.py'
>>>result = filename.startswith('test_') and filename.endswith('.py')
>>>result = result.replace('test_', '').replace('.py', '')
>>>print result
foo
答案 0 :(得分:6)
问题是你的模式匹配test_
之前和.py
之前的任何字符串,但这不会限制它在test_
之前或之后的其他字符。 .py
。
您需要使用开始(^
)和结束($
)anchors。另外,不要忘记逃避.
字符。试试这个模式:
(?<=^test_).+(?=\.py$)
答案 1 :(得分:1)
看看这个:
import re
files = [
"test_1.py",
"Test.py",
"test.pyc",
"test.py",
"script.py"]
print [x for x in files if re.search("^test_.*py$", x)]
输出:
['test_1.py']