开发者

help need to write regex [duplicate]

开发者 https://www.devze.com 2023-04-04 18:14 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: help need to write regex
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

help need to write regex

I have a logfile formatted as follows:

       Using data from (yyyy/mm/dd): 2011/8/3
       0 files queued for scanning.
       Warning: E:\test\foo
       Händler.pdf File not Found.
       Loading com, please wait. 
       1520 file scanned.

I want to write a regex to detect the Warning message and used date So my out put will be like { 'Used Date':'2011/8/3', 'Warning':'E:\test\foo Händler.pdf File not Found'}

I tried but I got only following output:

logd = re.compile("Using\sdata\sfrom\s\(yyyy/mm/dd\):\s(? P<Defs_Date>\d{4}/\d+/\d+)[^\w\d] ")
开发者_Go百科data =  Re.search(logd, log).groupdict()

Output will be :

{'Defs_Date': '2011/8/3'}

Can anybody help me update my regex to extract the information I'm looking for?


With out a broad view of your log file, this may help you:

In [1]: import re

In [2]: txt = open('foo.log', 'r').read()

In [3]: regexp = re.compile(r'''Using data.+\): (?P<Defs_Date>\d{4}/\d+/\d+).+(?P<Warning>Warning: .+)Loading.+scanned.''', re.S)

In [4]: regexp.search(txt).groupdict()
Out[4]: 
{'Defs_Date': '2011/8/3',
 'Warning': 'Warning: E:\\test\\foo\n       H\xc3\xa4ndler.pdf File not Found.\n       '}

Process the output to fit your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消