【发布时间】:2017-09-01 16:57:50
【问题描述】:
我想从标准 vsftp 日志文件中获取整个文件名和扩展名。
文件如下:
Wed Aug 31 10:23:59 2017 1 ::ffff:172.18.1.168 18593420 /Shell_Scripting.sh b _ i r user1 ftp 0 * c
Wed Aug 31 10:24:18 2017 1 ::ffff:172.18.1.168 18593420 /test.txt b _ i r user1 ftp 0 * c
我试过正则表达式
pattern = re.compile(r'\/(\w+)')
match = pattern.search(ftpfile)
print match.group(1)
但唯一匹配的文件名(Shell_Scripting & test)不包括扩展名(.sh & .txt)。
我试过re.compile(r'\/(.+\.\w+)')和re.compile(r'\/(\w+\.\w+)')
他们都显示AttributeError: 'NoneType' object has no attribute 'group'
什么应该是正确的正则表达式来匹配文件名包括文件扩展名?
【问题讨论】:
-
不要尝试正则匹配文件名。空间呢?本地文件系统允许的其他有趣的字符呢?多个
.ext.ens.ions怎么样?而是匹配到18593420的部分,然后是一组.+,然后匹配b _ i r user1 ftp 0 * c-part。 -
@user2722968 感谢提醒。是的,空格应该是个问题。我会尝试另一种方法