re = /<TEXT>\s*begin \d+ (?>[^e]+|e(?!nd\s*<\/TEXT>))*end\s*<\/TEXT>/m
str = '<HTML>
<BODY STYLE="font: 10pt Times New Roman, Times, Serif">
<TEXT>
begin 644 h52992h5299202.gif
M1TE&.#EA>P`>`,0``\']_?S\\_/^_O[P\\/#[^_OU]?7Q\\?\'X^/CY^?GZ^OK\\_/
MSV]O;T]/3]_?WR\\O+U!04````/___P``````````````````````````````
M`````````````````````````"\'Y!```````+`````![`!X```7_8"2.9&F>
M:*JN+"DP22O/=&W?Z`(M>.__0-4`T@@:C\\A689=L-0B$!`(!`""@S:P)$M"B
M!(#"$$(NEP,`0E,AR!X@"N_I`1D$&-6JM,H8PXX-$`5-`@9=<B8(#\'$K5$,\'
M1H$0:D@,$#&(*PD`*0(.<$8!@D@*$`:9*P)D*@D0G$&?"$A+KZ@H;PPJ;[4^
MI09M1Z)%MBB6E"<(ID8&9,!!`G;$7T-?2P[./F]DLD8`$)`F"@0\'",->;X<E
M")\\,V#W0`T.\\/IX#YA$"8F2BEW)+/",:(!#EX!@0\'0!:S>OQ!F"$!&+L%(C#
MS($<4<#`5#S@SD:#`@#:&#A%P%6W22):_QG@J(`/&7`N`#@`U[+`J0A4`-P[
M`0V"B`,&!A2`^>.%F5\\*@S08*4+!$`,`%HS9$4/``2MAIA:P=(;?O@0=?T*P
M&&%;DP1>XT%H]P/MO@!3S;P-P$RNF0$+$+3:!X!!7:@=EV`BXQ!)L@()I)*Q
MPTT&@@(!O)K"<X``5[D.%E2YDN!O90+F7I(8N)C1"&%BQQ:X$@P"L%:11[5`
M4%=NX8!0")@N`8:MB0*W11#X-("7@W3XELBU$SER8QFETI5<F$(!,Z$<(WB[
M*2W"1Z(F31#(`[DYLT$T1)F>?AIYB5)<G*D:8+`["?8T"(3M%&U$4A%1I:#-
M??%09Y]V*/5`VU0*WF`B@@X.KN"4(%!X0]^!*\'A37PT*$\'6")?(-L9,*$Y8Q
MP&X8DJ##B$DL04(IZ,U``&0&%+!?BH9D$D4)V:68A"KN^2BD#R4%-^21-O2(
\'Y)(TA```.S\\_
`
end
</TEXT>
<TEXT>losses occurring in the third quarter and from weather </TEXT>
</BODY>'
subst = '<TEXT><\\/TEXT>'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html