[Tutor] extracting lines between patterns.

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue May 15 19:41:36 CEST 2012

> Friends,
>Could someone please give some hint on how to extract lines between two patterns in a file. I use the re module to compile my patterns but not able to figure out how i can use these patterns to extract the lines lying in between.

Not sure exactly what you want but something like this could work. Note, this
is not memory efficient. 

line = file.read()
index1 = line.find( PATTERN1 )
index2 = line.find( PATTERN2 )
in_between = line[index1 + len( index1 ):index2]
number_of_lines = len( in_between.split() )  # -1 ?
# might need to adjust (-1) to avoid the line PATTERN1 is on


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

More information about the Tutor mailing list