python一个正则表达式的写法问题
程序log里有一句
INFO: Succesfully backed up database as file001
然后代码
m = re.search("Succesfully backed up database as (\S+)", stdout)
db_backup_name = m.group(1)
可以捕获到file001
另一个log里
INFO: Backup started to:
/u01/app/oracle/mysql/dbbackup/file002
如何捕获 file002这个文件名,正则如何写呢?
[解决办法]
>>> import re
>>> s = """INFO: Backup started to:
/u01/app/oracle/mysql/dbbackup/file002
Job: 'Start Backup to: Backup'"""
>>> pattern = r'INFO:\s+Backup started to:[\s\r\n]+(\S+)'
>>> m = re.search(pattern, s)
>>> m.group(1)
'/u01/app/oracle/mysql/dbbackup/file002'
>>> import os.path
>>> os.path.basename(m.group(1))
'file002'
>>>
[解决办法]
import re
s='''INFO: Backup started to:
/u01/app/oracle/mysql/dbbackup/file002
Job: 'Start Backup to: Backup
'''
l=re.findall(r'(?<=/)([^/]*?)(?=$)',s,re.MULTILINE)
print l