1

Here is my text file:

YN FILE=“stop.x" DATE=wwepweijfp Life=spfewff FUIL=“keener” CREATED_BY=“xxxxx” 
DY FD=4344334 HSH=OIHF 
DC A_GN=EFEFOK 
DF EFIIEFNFNEE
DF NIFENPEFEF
DF AO EIJHEPFN A0 pjwdwdwd
DF C0 STATICVECTOR 
ZZ ---------------< Fields >--------------------- 
DF A0 VERN FMGL ROJF AO
GS MOB40 99 
GA 4 Y 3 F 00H00M F 
G2 “eiberbnier       " EE 
C0 D56916E65D4500574124B15AD3226746A7F70AD7A3936A550BDB87FC077E9F4C7D36C4E9BFFA7545C4AE8C4173
A0 VDS1000170 34486EF018B1BF89 955486C529989CBC 89BCE463 919BC6E9 C927D0 09BA5356CD7CFFBD 
A0 VDS1000171 1951420D5909073A FF7B6A08E2BDC4AC E9593876 50ABACB9 894A6E 57AA4A23DE6414E0 

I would like to extract this block :

enter image description here

The first line of the text always starts with a YN FILE. I want to extract everything until it comes across a new line beginning with A0.

I have tried such :

matchObjj = re.search(r'YN.*\nC0.*\nA0', data, re.DOTALL)

But it also extracts the other A0 beginning in newline.

1 Answer 1

1

Beware, in a regex, * is gready. That means that if there are other lines starting with 'A0', it will stop at the last and not at the first. So you should use an ungreedy *?. And a group would be enough to get rid of the initial 'A0'. It could end with:

matchObjj = re.search(r'(YN.*?\n)A0', data, re.DOTALL)
block = matchObjj.group(1)
Sign up to request clarification or add additional context in comments.

1 Comment

That makes so much sense. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.