#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)(?#skip match process when S exist, or remove optional left most target T, capture left and right part of T." & @CRLF & _
"Use case: if N is not exist, modify/change T to N, or create N. If any create or modify write operated, appending output N to right." & @CRLF & _
"skip exist:)^(?!.*S)(?#" & @CRLF & _
"captrue prefix :)^(.*?)(?#" & @CRLF & _
"optional target to remove:)(?:T|$)(?#" & @CRLF & _
"captrue postfix:)(.*?)$(?#" & @CRLF & _
"Test string:" & @CRLF & _
"aa" & @CRLF & _
"aaT" & @CRLF & _
"Tcc" & @CRLF & _
"aaTcc" & @CRLF & _
"aaTbbTcc" & @CRLF & _
"aaS" & @CRLF & _
"aaTS" & @CRLF & _
"TccS" & @CRLF & _
"aaTccS" & @CRLF & _
"Saa" & @CRLF & _
"SaaT" & @CRLF & _
"STcc" & @CRLF & _
"SaaTcc" & @CRLF & _
"Substitution: $1$2N" & @CRLF & _
"Return:" & @CRLF & _
"aaN" & @CRLF & _
"aaN" & @CRLF & _
"ccN" & @CRLF & _
"aaccN" & @CRLF & _
"aabbTccN" & @CRLF & _
"aaS" & @CRLF & _
"aaTS" & @CRLF & _
"TccS" & @CRLF & _
"aaTccS" & @CRLF & _
"Saa" & @CRLF & _
"SaaT" & @CRLF & _
"STcc" & @CRLF & _
"SaaTcc)"
Local $sString = "aa" & @CRLF & _
"aaT" & @CRLF & _
"Tcc" & @CRLF & _
"aaTcc" & @CRLF & _
"aaTbbTcc" & @CRLF & _
"aaS" & @CRLF & _
"aaTS" & @CRLF & _
"TccS" & @CRLF & _
"aaTccS" & @CRLF & _
"Saa" & @CRLF & _
"SaaT" & @CRLF & _
"STcc" & @CRLF & _
"SaaTcc"
Local $sSubst = "$1$2N"
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