I get the line <image_name>NAME</image_name> , NAME is always different, how do I select it in a separate line. Saw that I use on C# Regex.Matches , but how to remake for with ++ did not understand. Give an example, please.

 <status_code>200</status_code> <status_txt>OK</status_txt> <data> <image_name>O7Q24Hz</image_name> <image_filename>O7Q24Hz.png</image_filename> <image_type>png</image_type> <image_path>/images/2013/10/23/O7Q24Hz.png</image_path> <image_url> http://img.chaos-online.ru/images/2013/10/23/O7Q24Hz.png</image_url> <image_width>1920</image_width> <image_height>1080</image_height> <image_attr>width="1920" height="1080"</image_attr> <image_bytes>340794</image_bytes> <image_size>332.8 KB</image_size> <image_thumb_url> http://img.chaos-online.ru/images/2013/10/23/O7Q24Hz.th.png</image_thumb_url> <image_thumb_path>/images/2013/10/23/O7Q24Hz.th.png</image_thumb_path> <image_thumb_width>240</image_thumb_width> <image_thumb_height>135</image_thumb_height> <image_id_public>Ix</image_id_public> <image_viewer> http://img.chaos-online.ru/image/Ix</image_viewer> <image_shorturl> http://img.chaos-online.ru/Ix</image_shorturl> <image_delete_hash>3l3xiEayJpXr6oeGLYgkQKuuJA</image_delete_hash> <image_delete_url> http://img.chaos-online.ru/delete/image/Ix/3l3xiEayJpXr6oeGLYgkQKuuJA</image_delete_url> <image_delete_confirm_url> http://img.chaos-online.ru/delete-confirm/image/Ix/3l3xiEayJpXr6oeGLYgkQKuuJA</image_delete_confirm_url> <image_date>2013-10-23 00:32:27</image_date> <source>base64 image string</source> <resized>0</resized> <shorturl> http://goo.gl/3HF2o9</shorturl> </data> 

</ response>

  • Are you very sure that it is necessary to parse regular expressions? any sane xml parser will work here at 100%. Even at 146%! - KoVadim

1 answer 1

Why so hard? If your format is so fixed, stupidly drop the first 12 and last 13 characters, that's all.

Save complex solutions for complex tasks.


If you really need to parse XML, then it is better to do it with the XML parser (unexpectedly, right?).

Something like

 auto xdoc = XDocument::Parse(gcnew String(ваша строка здесь)); // не забудьте оставить в строке корневой элемент auto imageNameElement = Enumerable::Single(xdoc->Root->Elements("image_name")); auto imageName = imageNameElement->Value; 

If you are working with native C ++, you would need to use a third-party library for working with XML. As your project receives XML from somewhere, it’s likely that some library is already in use. If not, take whatever you like. For example, TinyXML.

  • The answer that I get contains a lot of parameters 1 image, the name is one of them, I need to pull out 5 parameters from there, so dropping will not help me - Zwei
  • @Zwei, describe the entire data format, one line or several, the order of the tags is always fixed or not, etc. Otherwise, I'm afraid you will not get sensible advice. And so, look at man 3 regex, man 7 regex , IMHO there everything is described in detail. I think in Windows they db. raalizovanny. - avp
  • @VladD, generally correctly says that XML is better to parse. But, in principle, if this XML is in one line and the format does not change, then you can, of course, in simple terms, something like: #define TAG "<image_name>" char * get_image_name (const char * xml) {char * p = strstr (xml, TAG); if (p) {p + = sizeof (TAG) - 1; char * q = strchr (p, '<'); // we assume that there is no symbol in the body <int l = q - p + 1; char res [l]; strncpy (res, p, l - 1); res [l - 1] = 0; p = strdup (res); } return p; } - avp
  • @avp: if the format is stable, you can simplify, of course. TC should clarify the problem. - VladD