Help with regex needed

Yuri Slobodyanyuk yuri.slobodyanyuk at gmail.com
Tue Apr 12 01:20:00 EDT 2011


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

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

--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110412/25701c3b/attachment.html>


More information about the Python-list mailing list