Help with regex needed
Chris Rebert
clp2 at rebertia.com
Tue Apr 12 01:42:05 EDT 2011
On Mon, Apr 11, 2011 at 10:20 PM, Yuri Slobodyanyuk
<yuri.slobodyanyuk at 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
>
> 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]) + " +")
^^^^^^
This trailing " +" *requires* that the lines have trailing spaces. Do
they? Such files typically don't, and your example input doesn't
either (although that may be due to email formatting lossage).
Cheers,
Chris
--
http://blog.rebertia.com
> 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
More information about the Python-list
mailing list