I want to parse some kind (or pure) XML code from a QString.

My QString is like:

<a>cat</a>My cat is very nice.

I want to obtain 2 strings:

cat, and My Cat is very nice.

I think a XML parser is not maybe necessary, but in the future I will have more tags in the same string so it's also a very interesting point.

+2  A: 

You could use a regular expression <a>(.*)</a>(.*).

If you use Boost you could implement it like follows:

boost::regex exrp( "^<a>(.*)</a>(.*)$" );
boost::match_results<string::const_iterator> what;
if( regex_search( input_string, what, exrp ) ) {
    std::string tag( what[1].first, what[1].second );
    std::string value( what[2].first, what[2].second );
Kirill V. Lyadvinsky
input_string can be a QString?
I'm not sure. But you can get `std::string` from `QString` by using [`QString::toStdString`]( method.
Kirill V. Lyadvinsky
+3  A: 

In Qt you have the QRegExp class that can help you to parse your QString.

According to Documentation example:

 QRegExp rxlen("^<a>(.*)</a>(.*)$");
 int pos = rxlen.indexIn("<a>cat</a>My cat is very nice.");
 QStringList list
 if (pos > -1) {
     list << = rxlen.cap(1); // "cat"
     list << = rxlen.cap(2); // "My cat is very nice."

The QStringList list will contain the cat and My cat is very nice.

Patrice Bernassola
I read the documentation, but could you give me a simple example like boost example with QRegExp? Thanks!
It would be something like str = "Nokia Corporation\\tNorway"; QString company, web, country; rx.setPattern("^([^\t]+)\t([^\t]+)\t([^\t]+)$"); if (rx.indexIn(str) != -1) { company = rx.cap(1); web = rx.cap(2); country = rx.cap(3); }?