python一个正则表达式的写法问题

ladofwind 2014-01-23 05:20:04
程序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这个文件名,正则如何写呢?

...全文
203 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ladofwind 2014-01-24
引用 3 楼 angel_su 的回复:
>>> 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' >>>
感谢,正则太费神了,记不住那些规则,而且能活用这些规则不容易
  • 打赏
  • 举报
回复
chuifengde 2014-01-24
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
  • 打赏
  • 举报
回复
angel_su 2014-01-23
>>> 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' >>>
  • 打赏
  • 举报
回复
ladofwind 2014-01-23
引用 1 楼 angel_su 的回复:
可以用一样的方式,先取得整个路径,再os.path.basename()处理一下...
没找到取整个路径的好办法,主要还有回车换行, 下面还有些内容 INFO: Backup started to: /u01/app/oracle/mysql/dbbackup/file002 Job: 'Start Backup to: Backup' 大概log是上面这样
  • 打赏
  • 举报
回复
angel_su 2014-01-23
可以用一样的方式,先取得整个路径,再os.path.basename()处理一下...
  • 打赏
  • 举报
回复
发帖
脚本语言
加入

3.7w+

社区成员

JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • ITBOB • 鲍勃
申请成为版主
帖子事件
创建了帖子
2014-01-23 05:20