Skip to content Skip to sidebar Skip to footer

Extract Text Information Between Two Define Text

I have big text file which has lot of text information but I would like to extract the text between two defined text. e.g /begin MEASUREMENT XYZ UBYTE _CNV_A_R

Solution 1:

Use (?s), this is consider multiple line as a single line. So dot match all characters including newlines.

pattern = re.compile(r'(?s)begin MEASUREMENT[\s](.*?)end MEASUREMENT')

So try this,

import re
path = "py.txt"
file = open(path, 'r')
lines = file.read()
pattern = re.compile(r'(?s)begin MEASUREMENT[\s](.*?)end MEASUREMENT')
result = re.findall(pattern, lines)
print result[0]

EDITED

t = "XYZ"
pattern = re.compile(r'(?s)begin MEASUREMENT\s+((%s).*?)end MEASUREMENT'%t)

Solution 2:

Try this:

text ="""
    /begin MEASUREMENT XYZ
        UBYTE
        _CNV_A_R_LINEAR_____71_CM
        1
        100.
        -40.
        160.
        FORMAT "%3.0"
        SYMBOL_LINK "XYZ" 0
/begin IF_DATA EVTRKMNBXERTBK 
    DEFAULT_RASTERS 3 3
/end IF_DATA 
    /end MEASUREMENT"""print text.split("/begin MEASUREMENT")[1].split("/end MEASUREMENT")[0]

Post a Comment for "Extract Text Information Between Two Define Text"