#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?xm)\[\ (RUNNING|STOPPED)\ \]" & @CRLF & _
"(.+?)" & @CRLF & _
"(?:\[Pid:\ (\d+)\]|$)"
Local $sString = "" & @CRLF & _
"" & @CRLF & _
"I want to extract the words between the two bracket "blocks" and also the word in first brackets (RUNNING or STOPPED)." & @CRLF & _
"" & @CRLF & _
"Example (extract the bolded part):" & @CRLF & _
"" & @CRLF & _
"[ RUNNING ] My First Application [Pid: 4194]" & @CRLF & _
"" & @CRLF & _
"[ RUNNING ] Second app (some data) [Pid: 5248]" & @CRLF & _
"" & @CRLF & _
"[ STOPPED ] Logger App" & @CRLF & _
"" & @CRLF & _
"So, as you can see, the [Pid: X] part is optional. I can write the regex as follows:" & @CRLF & _
"" & @CRLF & _
"\[\s+(RUNNING|STOPPED)\s+\]\s+([^\[]+).*" & @CRLF & _
"" & @CRLF & _
"and it will work. But this would fail if App name would contain the '[' character. I tried the following, but it won't work:" & @CRLF & _
"" & @CRLF & _
"\[\s+(RUNNING|STOPPED)\s+\]\s+(?!\[Pid)+.*" & @CRLF & _
"" & @CRLF & _
"My idea was to match any words/characters that are not starting with "[Pid", but I guess this would match any words that are not followed by "[Pid"." & @CRLF & _
"" & @CRLF & _
"Is there any way to do exactly that: Match any word that is not "[Pid", i.e. match the part until first appearing of "[Pid" substring?" & @CRLF & _
""
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