user2030113
user2030113

Reputation: 485

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_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

i.e /begin MEASUREMENT and /end MEASUREMENT in between this I want to extract text.

My code is:

import re
path = r"d:\xyz.txt"
file = open(path, 'r')
lines = file.read()
pattern = re.compile(r'begin MEASUREMENT[\s][\w+](.*?)end MEASUREMENT')
print re.findall(pattern, lines)

Upvotes: 0

Views: 110

Answers (2)

mkHun
mkHun

Reputation: 5927

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)

Upvotes: 1

tfv
tfv

Reputation: 6259

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]

Upvotes: 0

Related Questions