Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Solved: Regex expression ?? <tag>...</tag>
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    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();
        linestr=output.mid(index,length);
        qDebug() &lt;&lt; "linestring = " << linestr;
        lineslist.append(linestr);
        index = exp.indexIn(output, index + length);
    }
    

    @

    1 Reply Last reply
    0
    • N Offline
      N Offline
      N3Roaster
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wurstinator
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • N Offline
            N Offline
            N3Roaster
            wrote on last edited by
            #5

            You want the setMinimal() method in QRegExp.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SherifOmran
              wrote on last edited by
              #6

              thank you guys, i found i

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

              But this outputs ex:
              "<line>بببببببببيب</span>"

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

              1 Reply Last reply
              0
              • N Offline
                N Offline
                N3Roaster
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SherifOmran
                    wrote on last edited by
                    #9

                    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>

                    Guys,

                    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

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SherifOmran
                      wrote on last edited by
                      #10

                      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?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved