To get data using Regex between 2 keywords

Hi I am trying to get the data between “MAIN ENGINE” and “AUXILIARY” using Regex (I needed to get the data highlighted in bold). This is a text I copy from a variable output

MAIN ENGINE 2 x Diesel - WinGD 6X72DF - 2-stroke 6-cyl. 720mm x3086mm bore/stroke 26,530mkW total at 69.10rpm.AUXILIARY 2 x Aux. Diesel Gen. - 4-stroke driving 2 x HD Hyundai Electric HSJ9 811-10P - AC generator(s) at 7,380ekW total, (9,225kVA total) 6600V at 50Hz. 2 x Aux. Diesel Gen. - 4-stroke driving 2 x HD Hyundai Electric HSJ9 803-10P - AC generator(s) at 5,540ekW total, (6,925kVA total) 6600V at 50Hz.PROPULSOR 2 x FP Propeller (Aft) (mechanical), 69.10rpm.OTHER ENGINE EQUIPMENT 2 x Screw Shaft.ENVIRONMENTAL EQUIPMENT 2 x Selective Catalytic Reduction - NOx - Unknown. 2 x BWTS - Ballast Water Treatment System - SunRui BC-3000 at 3000cu.m/hr.EMERGENCY 1 x Emergency Diesel Gen. - 4-stroke driving 1 x ac generator(s) at 50Hz.

Appreciate any help! thanks in advance!

Hi @Irfan_Musa

Try this

(?<=MAIN ENGINE\s+).*(?=AUXILIARY)

@Irfan_Musa

System.Text.RegularExpressions.Regex.Match(Input,"(?<=MAIN ENGINE\s+).*(?=AUXILIARY)").Value

Hope this helps!!

@Irfan_Musa

system.text.regularexpressions.regex.match(inputstr,“(?<=MAIN ENGINE).*(?=AUXILIARY)”).value.trim

1 Like

Hi @Irfan_Musa

Input= "MAIN ENGINE 2 x Diesel - WinGD 6X72DF - 2-stroke 6-cyl. 720mm x3086mm bore/stroke 26,530mkW total at 69.10rpm.AUXILIARY 2 x Aux. Diesel Gen. - 4-stroke driving 2 x HD Hyundai Electric HSJ9 811-10P - AC generator(s) at 7,380ekW total, (9,225kVA total) 6600V at 50Hz. 2 x Aux. Diesel Gen. - 4-stroke driving 2 x HD Hyundai Electric HSJ9 803-10P - AC generator(s) at 5,540ekW total, (6,925kVA total) 6600V at 50Hz.PROPULSOR 2 x FP Propeller (Aft) (mechanical), 69.10rpm.OTHER ENGINE EQUIPMENT 2 x Screw Shaft.ENVIRONMENTAL EQUIPMENT 2 x Selective Catalytic Reduction - NOx - Unknown. 2 x BWTS - Ballast Water Treatment System - SunRui BC-3000 at 3000cu.m/hr.EMERGENCY 1 x Emergency Diesel Gen. - 4-stroke driving 1 x ac generator(s) at 50Hz."
Output= System.Text.RegularExpressions.Regex.Match(Input,"(?<=MAIN ENGINE\s*).*(?=AUXILIARY)").Value

Hope it helps!!

@Irfan_Musa

input_str=" MAIN ENGINE 2 x Diesel - WinGD 6X72DF - 2-stroke 6-cyl. 720mm x3086mm bore/stroke 26,530mkW total at 69.10rpm .AUXILIARY 2 x Aux. Diesel Gen. - 4-stroke driving 2 x HD Hyundai Electric HSJ9 811-10P - AC generator(s) at 7,380ekW total, (9,225kVA total) 6600V at 50Hz. 2 x Aux. Diesel Gen. - 4-stroke driving 2 x HD Hyundai Electric HSJ9 803-10P - AC generator(s) at 5,540ekW total, (6,925kVA total) 6600V at 50Hz.PROPULSOR 2 x FP Propeller (Aft) (mechanical), 69.10rpm.OTHER ENGINE EQUIPMENT 2 x Screw Shaft.ENVIRONMENTAL EQUIPMENT 2 x Selective Catalytic Reduction - NOx - Unknown. 2 x BWTS - Ballast Water Treatment System - SunRui BC-3000 at 3000cu.m/hr.EMERGENCY 1 x Emergency Diesel Gen. - 4-stroke driving 1 x ac generator(s) at 50Hz."

System.Text.RegularExpressions.Regex.Match(input_str,"(?<=MAIN ENGINE\s*).*(?=AUXILIARY)").Value

Hi
Try This Expression :

System.Text.RegularExpressions.Regex.Match(“InputString”,“(?<=MAIN ENGINE\s+).*(?=AUXILIARY)”).Value

ensuring that the string also is kept when spanning over line breaks (as . is exluding \n) we can use the following pattern

(?<=MAIN ENGINE)[\s\S]+?(?=AUXILIARY)