help need to write regex in optional condition [Close]

后端 未结 2 375
闹比i
闹比i 2021-01-29 11:26

I have a logfile having contains as below

log=

       Using data from (yyyy/mm/dd): 2011/8/3
       0 files queued for scanning.
       Warning: E:\\tes         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 11:42

    The following works fine for me:

    txt = open("test.txt").read()
    print txt
    
    import re
    logdate = re.compile("Using.*: (?P\d{4}/\d+/\d+)")
    logwarn = re.compile("Warning: (?P.*)")
    dates = re.search(logdate, txt).groupdict()
    warns = re.search(logwarn, txt).groupdict()
    print dates
    print warns
    

提交回复
热议问题