#include <StringConstants.au3> ; to declare the Constants of StringRegExp
#include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate
Local $sRegex = "(?m)^(?:(?:[\s\w]*(?:({(?:[\s\w]*%\(\w*\)[\s\w]*)+(?:{(?:[\s\w]*%\(\w*\)[\s\w]*)+})*})+)[\s\w]*)+)$"
Local $sString = "# Your valid example" & @CRLF & _
"abc {st1 %(ph1) st11} int {st2 %(ph2) st22}{st3 %(ph3) st33 {st31 %(ph4) st332}} cd" & @CRLF & _
"" & @CRLF & _
"# Your valid Legenda" & @CRLF & _
"OptionalStaticText{OptionalStaticText %(Placeholder) OptionalStaticText {OptionalSubSection} OptionalStaticText} OptionalStaticText" & @CRLF & _
"" & @CRLF & _
"# Your invalid example" & @CRLF & _
"abc {st1 %(ph1) st11} int {st2 %(ph2) st22}{st3 %(ph3) st33 {st31 %(ph4) st332}} c-d" & @CRLF & _
"" & @CRLF & _
"" & @CRLF & _
"# Without pre / post optional static text and some optional text inside section" & @CRLF & _
"{%(Placeholder) OptionalStaticText {OptionalSubSection}}" & @CRLF & _
"" & @CRLF & _
"# With only a placeholder in the section" & @CRLF & _
"OptionalStaticText{%(Placeholder)} OptionalStaticText" & @CRLF & _
"" & @CRLF & _
"# The valid version" & @CRLF & _
"abc {st1 %(ph1) st11} int {st2 %(ph2) st22}{st3 %(ph3) st33 {st31 %(ph4) st332}} cd" & @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