Solved: Regex expression ?? <tag>...</tag>

  • Hello guys,
    I am trying to catch a string from inside another string. The string to be catched is as follows:

    <line> ..... some text .... </spin>

    I want to catch the some text. Could you please help me with the regexp?

    I tried the following but does not work

    QString output="embed'><span class=SpellE><span lang=AR-SA style=' '><line>نتمكيبسشبسشكنتمنسيشم</span></span>";

    QRegExp exp ("<line>.*</spin>");
    int index = exp.indexIn(output);
    qDebug () << "regexp" << output << "index=" &lt;&lt; index;
    QStringList lineslist;
    QString linestr;
    while (index &gt;= 0) {
        int length = exp.matchedLength();
        qDebug() &lt;&lt; "linestring = " << linestr;
        index = exp.indexIn(output, index + length);


  • You have </span> in your string instead of </spin> or you have </spin> in your regex instead of </span>.

  • writing mistake, it is </span> .. thank you for the note. I fixed it
    QRegExp [removed]"<line>.*</span>");

    how ever this does get all the text for example, if i have <line> ..... </span> <span1> </span>

    It produces all the text till the last </span> but i need the first </span> after the <line> tag

  • I don't know if Qt supports it, but .*? is a non-greedy expression which should be what you are searching for.

  • You want the setMinimal() method in QRegExp.

  • thank you guys, i found i

    QRegExp [removed]"<line>[^<]*</span>");

    But this outputs ex:

    is it possible to trim the <line> and the </span> in the same expression?
    to spare an extra line QString.mid ...

  • See the section in the QRegExp documentation on capturing text. The first paragraph shows how you can replace the call to mid with a call to exp.cap(). Alternately you could adjust the parameters on the call to mid since you want to strip out fixed length segments at the ends.

  • Wouldn't it make much more sense to use a real parser, like the XML module in Qt? RegExps are useful, but they are a poor substitute for parsers. What happens, for example, if your input text has multiple <span></span> pairs? What if these are nested?

  • Andre: thank you for your comment, however it does not need an xml parser, since i don't have multiple nested tags. Just only <line> and the required text is till the next first </span>


    I have the string now in the ascii format, I need to convert it to the QString type so that i can match the characters with each pressed key in my game.

    @"نتمك" @

    Edited: these vaues after &# are decimal representation of unicode values

  • can somebody help me in the last point?

    I need to split the numbers, convert each to hex

    int a1 = 1606;
    QString b = QString::number( a1 , 16 ); // b=0628

    put \x before and after the 2nd digit to get \x06\28

    QString test1 = QString::fromUtf8("\x06\x28"); // è encoded in UTF8

    is there an easier way to do it?

Log in to reply

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