Your regex seems working well.
You may make it somewhat more efficient if you "unroll" the first lazy dot pattern like
/\A---\R(.*(?:\R(?!---\R).*)*\R)?---\R(?s)(.*)\Z/
See the regex demo. Note: no mofifiers are necessary, there is one inline (?s)
modifier inside the pattern.
Details
\A
- start of string
---\R
- a full ---
line with a linebreak
(.*(?:\R(?!---\R).*)*\R)?
- an optional Capturing group 1:
.*
- the whole line
(?:\R(?!---\R).*)*
- 0 or more repetitions of
\R(?!---\R)
- a line break that is not followed with a ---
line followed with a linebreak
.*
- the whole line
\R
- a linebreak sequence
---\R
- a full ---
line with a linebreak
(?s)
- an inline DOTALL modifier making the dots to the right match line break chars
(.*)
Group 2: any 0+ chars as many as possible
\Z
- end of string.