What is a regex expression for CDATA

Hi, I have a CDATA example here

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

and

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

CDATA regular expression cannot recognize this

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

it doesn't work either

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

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

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

+3
source share
6 answers

This is the solution. The reason that we need to use the START STATE, - is that ever between <!CDATA[and ]]>did not get a match with another REGEX.

%option noyywrap
%x CDATA

%%
"<![CDATA[" { BEGIN CDATA; printf("Entering CDATA\n"); }
<CDATA>([^\]]|\n)*|.    { printf("In CDATA: %s\n", yytext); }
<CDATA>"]]>" { 
    printf("End of CDATA\n");
    BEGIN INITIAL;
}

%%
main()
{
    yylex();
}
+1
source

Easy enough, it should be like this:

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

At least he works at regexpal.com

+6

, , lex; , ERE, :

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

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

( , . , , , .)

, , , , C lex, , CDATA ( <![CDATA[), , ]]>, . ( , , flex lex ), , .

, , , , ], ]>. , ( !) , .

+3

, SO , HTML .NET.

CDATA .

CHAD:

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

:

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

:

asd[f]

FlexRegEx .

0
source

One note - searching for CDATA should also exclude comments; CDATA can be embedded.
/<!(?:\[CDATA\[(.*?)\]\]|--.*?--|\[[A-Z][A-Z\ ]*\[.*?\]\])>/sg
This can be done by checking if group 1 is valid for each match returned in the global search.

0
source
<!\[CDATA\[\s*(?:.(?<!\]\]>)\s*)*\]\]>

Previuos answer just changed

0
source

Source: https://habr.com/ru/post/1783885/


All Articles