[Tutor] Match on current line and next line. Possible?

Jacob S. keridee at jayco.net
Wed Feb 9 05:29:39 CET 2005


It's getting late, so if someone already suggested something like this, just 
pretend to smack me across the face, and I'll flinch later...

import re
fi = open('/somefile','r')   ## Don't do readlines and bring the whole file 
in memory...
match1 = re.compile('^Python')
match2 = re.compile('^/tBLAH')
prevline = ''
for line in fi:
    if match1.search(line) and match2.search(line):
        do_something()
    prevline = line
fi.close()

HTH,
Jacob


> Hello! How can I instruct Python to match on the current line and the
> next line?
>
>
> Assumptions;
> - We are reading in one line at a time
>
>
> BROKEN EXAMPLE (discussion)
> ######################
> file = open('/somefile','r').readlines()
> for line in file:
>        match_one = re.search('^Python', line)
>        match_two = re.search('^\tBLAH', line)
>        if match_one and nextline == match_two:
>               do_something()
>
>
> Maybe this just isn't possible, since we are working line by line.
>
> Any suggestions?
>
>
> Tom
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
> 



More information about the Tutor mailing list