#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?m)(?<=(?:\s|^)(?![@#])(?!__)\S*)_([^_]+)_"
Local $sString = "Test sentence:" & @CRLF & _
"_some_val #ta_g_ and _some_ value, but some_val_ and #some_tag_value and @a_mention_ and a #_tag_" & @CRLF & _
"" & @CRLF & _
"Should match (between underscores)" & @CRLF & _
"----" & @CRLF & _
"_italic text here_" & @CRLF & _
"police_woman_" & @CRLF & _
"_fire_fighter" & @CRLF & _
"a thousand _words_" & @CRLF & _
"_brunch_ on a Sunday" & @CRLF & _
"match_this_and_that_" & @CRLF & _
"" & @CRLF & _
"Should not match anything" & @CRLF & _
"----" & @CRLF & _
"@ta_g_" & @CRLF & _
"__value__" & @CRLF & _
"#some_tag_value" & @CRLF & _
"@some_value_here" & @CRLF & _
"@some_tag_" & @CRLF & _
"#some_val_" & @CRLF & _
"#_hello_" & @CRLF & _
"@match_this_and_that_"
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