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
The following works fine for me:
txt = open("test.txt").read()
print txt
import re
logdate = re.compile("Using.*: (?P<date>\d{4}/\d+/\d+)")
logwarn = re.compile("Warning: (?P<warn>.*)")
dates = re.search(logdate, txt).groupdict()
warns = re.search(logwarn, txt).groupdict()
print dates
print warns
One way to handle optional parts is to use regex text (optional part|)
. The part inside parenthesis will match either "optional part" (which can be a regex on its own) or nothing.