The problem is that your <text>
subpattern is false! You don't need to avoid ]
, you need to avoid ]
followed by ]>
. You can use this subpattern instead:
(?<text>(?>[^]]+|](?!]>))*)
the whole pattern: (note that many characters don't need to be escaped)
@"<!\s*\[CDATA\s*\[(?<text>(?>[^]]+|](?!]>))*)]]>"
I added two \s*
to match all your example strings, but if you want to disallow these optional spaces, you can remove the \s*
.