#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?mi)(?<=^CN=)(.*?)(?=\s?,\s?o)"
Local $sString = "CN= John Q. Public, O=Agency, C=US" & @CRLF & _
"CN= John Q. Public, Jr.,O=Agency, C=US" & @CRLF & _
"CN=John Q. Public,O=Agency, C=US" & @CRLF & _
"CN=John Q. Pu,blic,O=Agency, C=US" & @CRLF & _
"CN=some name,ou=Groups,ou=AAA,ou=DCCOMICS,o=boo berries,c=US" & @CRLF & _
"CN=some name, ou=Groups,ou=AAA,ou=DCCOMICS,o=boo berries,c=US" & @CRLF & _
"CN=some name-Y- , ou=Groups,ou=AAA,ou=DCCOMICS,o=boo berries,c=US" & @CRLF & _
"cn= some name-Y-,ou=Groups,ou=AAA,ou=DCCOMICS,o=boo berries,c=US" & @CRLF & _
"cn= some na, o me-Y-,ou=Groups,ou=AAA,ou=DCCOMICS,o=boo berries,c=US" & @CRLF & _
"" & @CRLF & _
"(?<=^CN=) - positive lookahead to find string starts with CN= and discard that match" & @CRLF & _
"(.*?) - non greedy seach for any single character" & @CRLF & _
"(?=,\s?ou\s=|,\s?o\s?=)" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"(?=,\s?ou\s=|,\s?o\s?=)"
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