#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?m)^(?:<#.*?#>|[^#])*(#.*)?"
Local $sString = "-----------------" & @CRLF & _
"---MATCH CASES---" & @CRLF & _
"------------------------------------------------" & @CRLF & _
"$text = "Hello"\t# Comment after tab" & @CRLF & _
"$text+= #Comment without space after hash" & @CRLF & _
"Write-Host $text# Comment without space before hash" & @CRLF & _
"#Comment line with no whitespaces around'" & @CRLF & _
"Write-Host "Hello"#Comment in the end with no whitespaces around'" & @CRLF & _
"# Comment with whitespaces around '" & @CRLF & _
"# Consecutive comments" & @CRLF & _
"# Last line comment without line ending in the end" & @CRLF & _
"$text+= #Comment with < inside" & @CRLF & _
"$text+= #Comment ending with >" & @CRLF & _
"$text+= #Comment with <# inline comment #>" & @CRLF & _
"# Normal comment" & @CRLF & _
"Write-Host "Comment without text" #" & @CRLF & _
"An <# inline comment #> preceding a #comment" & @CRLF & _
"An <# inline # comment containing a # #> preceding a #comment" & @CRLF & _
"Two <# inline #> comments <# to be #> skipped # comment" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"--------------------" & @CRLF & _
"---NO MATCH CASES---" & @CRLF & _
"------------------------------------------------" & @CRLF & _
"Write-Host "Hello"" & @CRLF & _
"Write-Host "World"" & @CRLF & _
"Write-Host "Non-empty line"" & @CRLF & _
"$text = "Hello" <# block comment #> + "World"" & @CRLF & _
"$text = "Hello" +<#comment#>"World"" & @CRLF & _
"<# Block comment in a line #>" & @CRLF & _
"Write-Host "Hello world <# Block comment in the end of line #>"
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