How to replace a word between two elements in a XML file

  • Hi, i'm working with the class QRegExp and i need to replace a word between two elements in a XML file, for example, i have this text

    	cvShowImage("ventana_con_AUTOSIZE", image);

    I need to replace the ampersand, or all ampersands, to & amp; between <Code> and </Code>, never before <Code> or after </Code>. I tried this

    QRegExp re_code"(?:<Code>)(?:.*)&(?!amp;)(?=.*</Code)"

    but it doesn't work, it replaces from <Code> to &. The result is this:


    I want to do the same with >, < and all illegal characters of XML that they have to be escaped.

    Thanks in advance

  • Moderators

    I am not sure of how you like to replace the character.
    However, to me it looks that your match of QRegExp is not correct. It is:

        cvShowImage("ventana_con_AUTOSIZE", image);

    If you replace this, the result is logical. The result of your regular expression you can easily check if you are using the QRegExp example.

  • @koahnig I replace it using this piece of code

        QFile f(dir);
        if(! {
            QMessageBox::information(0, "error", f.errorString());
        QTextStream in(&f);
        QString text= in.readAll();
        QRegExp re_code("(?:<Code>)(?:.*)&(?!amp;)(?=.*</Code)");
        text = text.replace(re_codigo,"&amp;");

  • Moderators

    The problem is the match as already described in my previous reply.

        QRegExp re_code("((?:<Code>)(?:.*))(&)(?!amp;)(?=.*</Code)");
        text = text.replace(re_code, re_code.cap(1) +"&amp;");

    You may want to try the code above. I have changed the last two lines only of your code block.
    The text replaced is still the same, but it adds the part which is missing.
    Note: this brain to keyboard and not really tested. It is not elegant in my opinion, but it is the only solution I see at the moment. Maybe someone with more regex experience has a better solution.

  • @koahnig Hi, i did something that it works, but is a botched job xD, maybe it is usefull for anyone who reads this post. Now i can lie to Sax parse

        QRegExp re_amp("&(?!.*<Code>)(?=.*</Code>)");
    //    QRegExp re_lt("(?!.*<Code>)<(?=.*</Code>)"); This works but i don't know why 
        QRegExp re_lt("<(?!.*<Code>)(?=.*</Code>)");
        QRegExp re_lt_fix("&lt;(?=Code>)");
        QRegExp re_gt(">(?!.*<Code>)(?=.*</Code>)");
        QRegExp re_gt_fix("<Code&gt;");

    I tried to use QRegularExpression but it works different, I'll try again in a future. Thank you very much anyway.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.