re = /(?s)(Object(?:(?!Object).)*?ByteCode[ \t]*:[ \t]*\R+)((?:(?:(?!EndByteCode).)*?(?:(?(3)(?!))(\b\w*?z\b)|(?(4)(?!))(?!\3\b)(\b\w*?z\b)|(?(5)(?!))(?!\b(?:\3|\4)\b)(\b\w*?z\b)|(?(6)(?!))(?!\b(?:\3|\4|\5)\b)(\b\w*?z\b)|(?(7)(?!))(?!\b(?:\3|\4|\5|\6)\b)(\b\w*?z\b)|(?(8)(?!))(?!\b(?:\3|\4|\5|\6|\7)\b)(\b\w*?z\b)|(?(9)(?!))(?!\b(?:\3|\4|\5|\6|\7|\8)\b)(\b\w*?z\b))){1,7})/m
str = 'BEFORE:
Object
Type : InfinityProgram
LastChange : 2/18/2025 5:15:38 PM
DeviceId : NetWork_MankatoO\\OfcDmp\\Vav5_26
Alias : RmTempSignalPRG
CreateTime : 2/18/2025 5:13:20 PM
CreatedBy : Root\\Acc
TimeLocked : 2/18/2025 5:15:38 PM
InstanceId : 7227/33
ByteCode :
\'ROOM TEMPERATURE CONTROL SIGNAL CALCULATIONS \' Dylan Salisbury Paape Energy Services 5/19/23
Numeric sync, lasthour
Numeric actn \' control loop "action" 1 or 0 (direct or reverse)
Line StartUp
Goto RmHappy
Line RmHappy
If ((IntrLkSystemRun = On) & (TempRmAvg > (StptRoom + 1)) & (TS > 2)) then Goto RmWarm
TempRmCntrlSig = 50
DamperPositionz = .5
Line RmWarm
If ((IntrLkSystemRun = Off) or (TempRmAvg < StptRoom)) then Goto RmHappy
TempRmCntrlSigz = PID11Fct(TempRmAvg, StptRoom, PIDTR[1], PIDTR[2], PIDTR[3], 0.5, actn, PIDTR[4], PIDTR[5], 0.5, 1, TempRmCntrlSigz, 0.005, Date)
TempRmCntrlSig = (TempRmCntrlSigz * 100)
DamperPositionz = .25
TempRmCntrlSigzAAAz = XXX
EndByteCode
Status : Active
EndObject'
subst = '$1Numeric $3, $4, $5, $6, $7, $8, $9\\r\\n\\r\\n$2'
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