#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?s)\W*\b(?!foobar).+?\b\W*"
Local $sString = "this should be highlighted, and" & @CRLF & _
"same with this. but any sentence" & @CRLF & _
"that has the word" & @CRLF & _
"foobar" & @CRLF & _
"shouldnt be, and same for any regular" & @CRLF & _
"sentence with foobar <-- like that" & @CRLF & _
"foobar beginning a sentence should invalidate" & @CRLF & _
"the entire sentence, same with at the end foobar" & @CRLF & _
"only foobar, and nothing else of the sentence" & @CRLF & _
"more words here more irrelevant stuff to highlight" & @CRLF & _
"and nothing of the key word" & @CRLF & _
"what about multiple foobar on the same foobar 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