程序员经常会面临日志的分析工作。而正则表达式是处理日志的必备工具。
“Line 622: 01-01 09:04:16.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] update_freeze_data: dabc9:bl_level=1740”
“app_log_cat new log begin”
“Line627: 01-01 09:04:17.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] update_freeze_data: dabc:bl_level=1720”
比如,对于上面的日志,需要找到日志时间,并且要找到对应格式的数据。这里面包含的问题主要包括:
匹配工作。需要找到真正的日志,上面的第二行就不是真正的日志;
分割工作(split)。把日志按照空格进行分割,找到日志时间;
筛选工作。找到匹配的格式,从而把数字1740和1720筛选出来。
针对匹配工作,需要找到开头是 'Line' 的行。用到re的search()函数。
importre
strrs=list()
strrs.append("Line 622: 01-01 09:04:16.727 <6> [pid:14399, cpu1 dabc_pwym_task] [histbf] \
update_freeze_data: dabc9:bl_level=1740")
strrs.append("app_log_cat new log begin")
strrs.append("Line 627: 01-01 0