0

I have a string with:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <url>
        <![CDATA[http://site.ru/uploadImage?returnUrl=http%3A%2F%2Fwww.site.ru%2Fdk%3Fcmd%3DgrPostImageUploaded%26amp%3Btkn%3D3089&returnErrorUrl=http%3A%2F%2Fwww.site.ru%2Fdk%3Fcmd%3DgrPostImageUploaded%26amp%3Btkn%3D3916&flashLoad=on&photoId=]]>
    </url>
    <tokens>
        <token>
            <!CDATA[258xA2Z4KDWBBRo%2FBbZBLZqw%2F1adFyhFOsWKJTidWIgxfIJ8RWZYdpIsOJ3U6RevDsRBR7gKeG2V29evBOFXUaqTg%3D%3D]]>
        </token>
    </tokens>
    <maxImageSize>
        1024
    </maxImageSize>
</data>

I try to parse token><![CDATA[(.*)]] with code

string token = Regex.Match(upload_info, @"CDATA[(.*)]]></token").Groups[1].Value;

But token is empty. Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
odfls
  • 3
  • 1

1 Answers1

7

[(.*)] means "one character of the following: (, ., * or )".

You probably meant @"CDATA\[(.*?)\]\]></token".

Note the reluctant quantifier .*?, or you would match everything from the very first [CDATA to the very last ]]</token.

But you shouldn't be using a regex for parsing XML in the first place, especially not CDATA sections. That's what parsers are there for.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561