#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?m)^(?!_(?!_))(?!(?:.*[^_\n])?_$)[\w.-]+$"
Local $sString = "Valid Entries: " & @CRLF & _
"abc" & @CRLF & _
"ABC" & @CRLF & _
"Abc123" & @CRLF & _
"123a" & @CRLF & _
"Abc_12" & @CRLF & _
"Abc_12_3a" & @CRLF & _
"abc_23_43_fg" & @CRLF & _
"___should_work_but_doesnt__________" & @CRLF & _
"___ok__" & @CRLF & _
"___ok____" & @CRLF & _
"__a__" & @CRLF & _
"" & @CRLF & _
"Invalid Entries: " & @CRLF & _
"_abc" & @CRLF & _
"abc_" & @CRLF & _
"_abc_"
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