#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)^(?:[^_]*_){3}([^_]+_[^_]+)_(?:[^_]+_){3}([^_]+_[^_]+).*"
Local $sString = "a_b_c_d_e_f_g_1_2_3_4_5" & @CRLF & _
"_b_c_dd_ee_f_g_1_222_333_4_5" & @CRLF & _
"b_c_dd_ee_f_g_1_222_333_4_5" & @CRLF & _
"_c_dd_ee_ff_g_1_222_333_444_5" & @CRLF & _
"c_dd_ee_ff_g_1_222_333_444_55_66" & @CRLF & _
"_dd_ee_ff_gg_1_222_333_444_55_66"
Local $sSubst = "$1_$2"
Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst)
MsgBox($MB_SYSTEMMODAL, "Result", $sResult)
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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm