On Tue, Apr 12, 2011 at 10:50 AM, Yuri Slobodyanyuk <yuri.slobodyan...@gmail.com> wrote: > Good day everyone, > I am trying to make this pretty simple regex to work but got stuck, > I'd appreciate your help . > Task: Get current date , then read file of format below, find the line that > matches > the current date of month,month and year and extract the number from such > line. > Here is what I did , but if i run it at 11 April 2011 ... > - regex pattern_match matches nothing > - regex pattern_test matches the line "4141411 Fri 11 11 2011" , logical > as it is the last matching line in year 2011 with the date of 11th. > My question - why regex pattern_match does not match anything and how to > make it match the exact line i want. > Thanks > Yuri
Consider using datetime.strptime to parse dates and times. You will have to strip off the first column since it doesn't look like part of the date itself. > > from datetime import datetime, date, time > import re > today_day = datetime.now() > time_tuple= today_day.timetuple() > pattern_match = re.compile("([0-9])+ +" + "Fri +" + str(time_tuple[1]) + " > +" + str(time_tuple[2]) + " +" + str(time_tuple[0]) + " +") > hhh = open("file_with_data.data",'r') > pattern_test = re.compile("([0-9]+)" + ".*" + " +" + > str(time_tuple[2]).strip(' \t\n\r') + " +" + str(time_tuple[0]).strip(' > \t\n\r') ) > for nnn in hhh: > if (re.search(pattern_test,nnn)): > print nnn.split()[0] > > 1111111 Fri 4 8 2011 > 2323232 Fri 4 15 2011 > 4343434 Fri 4 22 2011 > 8522298 Fri 4 29 2011 > ......... > 5456678 Fri 10 28 2011 > 5633333 Fri 11 4 2011 > 4141411 Fri 11 11 2011 > 3324444 Fri 11 18 2011 > > -- > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list