#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?s)<!--(?:(?!<!--|-->).|(?R))*-->"
Local $sString = "Remove comments from some generated HTML which can be invalid with nested comments" & @CRLF & _
"==================================================================================" & @CRLF & _
"" & @CRLF & _
"I would like to remove HTML comments from some generated content." & @CRLF & _
"If I use the regex `/<!--(.*?)-->/` (ungreedy with the `?`) then it works for most cases such as this example:" & @CRLF & _
"" & @CRLF & _
"```html" & @CRLF & _
"<!-- <h1> test </h1> --> not remove <!-- <h1> test 2 </h1> -->" & @CRLF & _
"```" & @CRLF & _
"" & @CRLF & _
"It gets rid of the `<h1>` tags and leaves the "*not remove*" as desired." & @CRLF & _
"" & @CRLF & _
"But **if the comments are nested**, then it will not handle it properly as it will leave the last comment closing tag `'-->'`. The workaround would be to use a greedy pattern, but in this case it will not work for the first case, with multiple comments." & @CRLF & _
"" & @CRLF & _
"Example of nested comments (I know it's not valid HTML, but it's the backend which is generating it):" & @CRLF & _
"" & @CRLF & _
"```html" & @CRLF & _
"text <!-- something <!-- <p> test </p> --> need remove -->" & @CRLF & _
"```" & @CRLF & _
"" & @CRLF & _
"I've tried to find a solution, but I don't know how to solve this. Has anyone an idea how to handle it?" & @CRLF & _
"" & @CRLF & _
"<!-- multiline" & @CRLF & _
"comment -->" & @CRLF & _
"" & @CRLF & _
"<!-- <footer>Footer with" & @CRLF & _
" nested <!-- comment -->" & @CRLF & _
" on several lines.</footer>" & @CRLF & _
"-->"
Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYGLOBALFULLMATCH)
Local $aFullArray[0]
For $i = 0 To UBound($aArray) -1
_ArrayConcatenate($aFullArray, $aArray[$i])
Next
$aArray = $aFullArray
; Present the entire match result
_ArrayDisplay($aArray, "Result")
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