pretty code

2018年12月12日 星期三

Python 正規表示式用法

Python 在使用 re 時
記得 pattern 要包含其他需要的字串(.*)

舉例來說
我想尋找每行有 "ERROR " 字眼的字串
re pattern 要寫成 "ERROR .*"
否則找到的都會只有 "ERROR " 本身

底下是針對規則的進階用法
把想要特別提出來的寫成如下形式
(?P<name>正規表示式)

就可以更方便的找出對應的資料


import re

ERROR_PATTERN = 'ERROR.*Test: (?P<TEST>[0-9]+).*Address: (?P<ADDR>[0-9A-F]+)'

errMsg = '''
2068-09-12 22:06:48 - [MEM ERROR - Data] Test: 6, CPU: 1, Address: 101B8F330, Expected: FFFF5FFF, Actual: FFFFDFFF
2068-09-12 22:06:48 - [MEM ERROR - Data] Test: 6, CPU: 1, Address: 103B8F310, Expected: FFFF5FFF, Actual: FFFFDFFF
2068-09-12 22:06:50 - [MEM ERROR - Data] Test: 6, CPU: 3, Address: 119FEA5F0, Expected: FFFF7FDF, Actual: FFFFFFDF
2068-09-12 22:06:50 - [MEM ERROR - Data] Test: 6, CPU: 3, Address: 11BFEA5D0, Expected: FFFF7FDF, Actual: FFFFFFDF
2068-09-12 22:20:53 - [MEM ERROR - Data] Test: 6, CPU: 2, Address: 10466E930, Expected: FFFF7FEF, Actual: FFFFFFEF
2068-09-12 22:20:53 - [MEM ERROR - Data] Test: 6, CPU: 2, Address: 10666E910, Expected: FFFF7FEF, Actual: FFFFFFEF
2068-09-12 22:20:53 - [MEM ERROR - Data] Test: 6, CPU: 0, Address: 10D6CC9B0, Expected: FFFF7EFF, Actual: FFFFFEFF
2068-09-12 22:20:53 - [MEM ERROR - Data] Test: 6, CPU: 0, Address: 10F6CC990, Expected: FFFF7EFF, Actual: FFFFFEFF
'''

res = re.findall(ERROR_PATTERN, errMsg)

print(res)


輸出結果
[
    ('6', '101B8F330'), 
    ('6', '103B8F310'), 
    ('6', '119FEA5F0'), 
    ('6', '11BFEA5D0'), 
    ('6', '10466E930'), 
    ('6', '10666E910'), 
    ('6', '10D6CC9B0'), 
    ('6', '10F6CC990')
]

沒有留言: