#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?m)^(?P<indentation>(?P<sp_or_tab>[ \t])*+)(?P<stmt>(?P<non_str_lit>(?:(?!\#)[^\"'\r\n])*?)(?:(?P<str_lit>(?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\[^\r\n ]|[^\"'\\\r\n])*?(?P=begin_quote))(?&non_str_lit))*?)(?P<whitespace>(?&sp_or_tab)*+)(?P<comment>(?:\#)[^\r\n]*+)?(?P<line_ending>\r|\n|\r\n)?$"
Local $sString = "print("Hello, world!") # this is a comment." & @CRLF & _
"print("The quick \\brown\\\ndog\rfox\tjumps\t over #the# /lazy/ \"dog\"###") # and here's where the real comment starts" & @CRLF & _
" print("you can use Alt+0009 to enter a tab character!")" & @CRLF & _
"# The above line had no comments, so the <comment> group didn't participate in the match." & @CRLF & _
" print("indented code") # with comment" & @CRLF & _
" print("never indent code like this, but it still matches anyway")# comment lacking preceding whitespace >:)" & @CRLF & _
" print("space indented code")" & @CRLF & _
"print("Missing end quote" & @CRLF & _
"print("unescaped \")" & @CRLF & _
"print("implicitly" " joined## " "string " "lit#erals") # a line can contain multiple string literals" & @CRLF & _
"print("the quick " # line 1" & @CRLF & _
" "brown fox " # line 2" & @CRLF & _
" "jumps over " # line 3" & @CRLF & _
" "the lazy dog") # line 4" & @CRLF & _
"" & @CRLF & _
" print("The quick brown", animal, "jumps over the", adjective, animal2, sep=" ") "" & @CRLF & _
"print('Robert'); DROP TABLE `Students`; -- ')"
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