What is the regex expression for CDATA

Freddy Chua picture Freddy Chua · Jan 6, 2011 · Viewed 14.8k times · Source

Hi I have an example CDATA here

<![CDATA[asd[f]]]>

and

<tag1><![CDATA[asd[f]]]></tag1><tag2><![CDATA[asd[f]]]></tag2>

The CDATA regex i have is not able to recognize this

"<![CDATA["([^\]]|"]"[^\]]|"]]"[^>])*"]]>"

this does not work too

"<![CDATA["[^\]]*[\]]{2,}([^\]>][^\]]*[\]]{2,})*">"

Will someone please give me a regex for <![CDATA[asd[f]]]>, I need to use it in Lex/Flex

: I have answered this question, please vote on my answer, thanks.

Answer

Sean Patrick Floyd picture Sean Patrick Floyd · Jan 6, 2011

Easy enough, it should be this:

<!\[CDATA\[.*?\]\]>

At least it works on regexpal.com