#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)(?<=^|[^_])__((?![\s_])(([^_]|_(?!_))+?)?[^\s_])__(?=[^_]|$)"
Local $sString = "-- funciona" & @CRLF & _
"__foo bar__" & @CRLF & _
"__f o o b a r__" & @CRLF & _
"**teste __lorem ipsum__ dolor sit**" & @CRLF & _
"__teste **lorem ipsum** dolor sit__" & @CRLF & _
"abc__xyz__teste" & @CRLF & _
"" & @CRLF & _
"-- não funcionava na primeira versão" & @CRLF & _
"__foo_bar__" & @CRLF & _
"__a__" & @CRLF & _
"__*__" & @CRLF & _
"__foo_*bar__" & @CRLF & _
"" & @CRLF & _
"-- não é para funcionar" & @CRLF & _
"__ foo bar__" & @CRLF & _
"__foo bar __" & @CRLF & _
"_foo__" & @CRLF & _
"__ foo __" & @CRLF & _
"_foo_" & @CRLF & _
"__foo_" & @CRLF & _
"__ __" & @CRLF & _
"__a____" & @CRLF & _
"____a__" & @CRLF & _
"_____" & @CRLF & _
"" & @CRLF & _
"-- corner case: aninhado (não tratei pois não sei o que deveria fazer)" & @CRLF & _
"__abc__xyz__123__" & @CRLF & _
"__abc __xyz__ 123__" & @CRLF & _
""
Local $sSubst = "<i>\1<\i>"
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