zeekzhen
zeekzhen

Reputation: 159

python regex match content crossing multiple lines

I have a txt file, include multiple line.My result crossing multiple lines.

for example, my data can be simplified as the following:

target_str =

x:-2.12343234
aaa:-3.05594480202
aaa:-3.01292995004
aaa:-2.383299
456:-2.232342
x:-2.53739230
aaa:-2.96875038099
aaa:-2.92326261448
aaa:-2.87628054847
bbb:-2.82755928961
456:-2.77678240323
x:-2.3433210
aaa:-2.72356707049
aaa:-2.6675072938
aaa:-2.60827106148
456:-2.3323232
x:-2.8743920
aaa:-2.433233
aaa:-2.9747893
aaa:-2.9747893
bbb:-2.43873
456:-2.43434

I want to match

x:.....
aaa:.....
aaa:.....
aaa:.....
bbb:.....
456:.....

means if there exist bbb, then I pick up the lines from x:... to 456:....

The expected results for the example data is:

x:-2.53739230
aaa:-2.96875038099
aaa:-2.92326261448
aaa:-2.87628054847
bbb:-2.82755928961
456:-2.77678240323

x:-2.8743920
aaa:-2.433233
aaa:-2.9747893
aaa:-2.9747893
bbb:-2.43873
456:-2.43434

I write:

a=re.findall(r"x:(.*\n){4}bbb:.*\n456.*",target_str)

print(a)

But the results is:

['aaa:-2.87628054847\n', 'aaa:-2.9747893\n']

This is not correct, can anyone help me? thanks a lot.

Upvotes: 1

Views: 112

Answers (1)

setius666
setius666

Reputation: 46

Try with following regex:

(x:(?:.*\n){4}bbb:.*\n456.*)

(?:.*\n) - ?: Makes group non capturing, so it won't be set to output.

Adding parenthesses on whole regex makes it an group which you would like to see as output

Upvotes: 1

Related Questions