<div dir="ltr"><p>Good day everyone, <br>I am trying to make this pretty simple regex to work but got stuck, <br>I'd appreciate your help .<br>Task: Get current date , then read file of format below, find the line that matches<br>
the current date of month,month and year and extract the number from such line.<br>Here is what I did , but if i run it at 11 April 2011 ...<br> - regex pattern_match matches nothing<br> - 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.<br>
My question - why regex pattern_match does not match anything and how to make it match the exact line i want.<br>Thanks<br>Yuri</p>
<p>from datetime import datetime, date, time<br>import re<br>today_day = datetime.now()<br>time_tuple= today_day.timetuple()<br>pattern_match = re.compile("([0-9])+ +" + "Fri +" + str(time_tuple[1]) + " +" + str(time_tuple[2]) + " +" + str(time_tuple[0]) + " +")<br>
hhh = open("file_with_data.data",'r')<br>pattern_test = re.compile("([0-9]+)" + ".*" + " +" + str(time_tuple[2]).strip(' \t\n\r') + " +" + str(time_tuple[0]).strip(' \t\n\r') )<br>
for nnn in hhh:<br> if (re.search(pattern_test,nnn)):<br> print nnn.split()[0]</p>
<p>1111111 Fri 4 8 2011<br>2323232 Fri 4 15 2011<br>4343434 Fri 4 22 2011<br>8522298 Fri 4 29 2011<br>.........<br>5456678 Fri 10 28 2011<br>5633333 Fri 11 4 2011<br>4141411 Fri 11 11 2011<br>3324444 Fri 11 18 2011<br clear="all">
<br>-- <br><br></p></div>