#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)^((?:(?:[^"\n;]*"[^"\n]*")+(?!"))?+[^"\n;]*+"?[^"\n;]*);.*"
Local $sString = "Peekaboo ; A comment starts with a semicolon and continues till the EOL" & @CRLF & _
"Unless the semicolon is surrounded by dquotes "Don’t do it ; here" ;but match me; once" & @CRLF & _
"Im not surrounded "so pay attention to me" ; "peekaboo"" & @CRLF & _
"Im not surrounded "so pay attention" to;me" ; "peekaboo"" & @CRLF & _
"Im not surrounded "so pay attention to me ; peekaboo" & @CRLF & _
"Dquote escapes a dquote so "dont pay attention to ""me;here"" buster" do it ; here" & @CRLF & _
"Don’t pay attention to """me;here""" but do ""it;here"" " & @CRLF & _
"and "dont do ""it;here""" either ;peekaboo" & @CRLF & _
"but "pay attention to "it;here"" ;not here though" & @CRLF & _
"Simon said "I like goats" then he added "and sheep;" ;a good comment is "here" & @CRLF & _
"Simon said "I like goats" then he added "and sheep;" dont do it here" & @CRLF & _
"Simon said ""I like goats;"peekaboo" & @CRLF & _
"Simon said "I like goats;""peekaboo" & @CRLF & _
"" & @CRLF & _
"""I like goats;"peekaboo" & @CRLF & _
""I like goats;""peekaboo" & @CRLF & _
"" & @CRLF & _
""aaa ; bb""
Local $sSubst = "$1"
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