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] Parsing expressions in xml file

[solved] Parsing expressions in xml file

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 2.9k Views
  • 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.
  • A Offline
    A Offline
    abich
    wrote on last edited by
    #1

    hello,
    I use QRegExp to parse my expression.
    @QString expression = "len>=1";

    QRegExp xp("(\\w+)"); 
    QString clockname, op, ter;
    
    
    if (xp.indexIn(expression) != -1)
    {
         clockname = xp.cap(1); // clockname=len
         op=xp.cap(2);// op is empty
         ter=xp.cap(3);//ter is empty
    
    }@
    

    My problem is that I can get clockname content but for the other variables i have nothing: they are empty.
    I think my QRegExp is wrong, can any one help me?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MementoMori
      wrote on last edited by
      #2

      having the text "len>=1" what do you want inside clockname, op and ter variables ?

      1 Reply Last reply
      0
      • X Offline
        X Offline
        Xander84
        wrote on last edited by
        #3

        Hi, first if you are using Qt 5+ you should use QRegularExpression instead of QRegExp. QRegularExpression is easier to use, more standard compliant to the pear regex syntax and also faster (I think).

        Ok about your regex string, it would help to know how your input can look like, maybe provide some more examples instead of just one? What I see you are just trying to use a "string split" with "\w" that is just word character (letters, numbers and _ as far as I know).
        Also you are using the "indexIn" method, I don't hink that is what you want, you should check the introduction and examples in the documentation first: http://qt-project.org/doc/qt-5/qregularexpression.html#details

        1 Reply Last reply
        0
        • A Offline
          A Offline
          abich
          wrote on last edited by
          #4

          using the expression "len>=1" i want to have "len" inside clockname, ">=" inside op and "1" inside ter.

          PS: i have Qt 4

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xander84
            wrote on last edited by
            #5

            ok then you have to stick with QRegExp for now..
            anyway we don't know much about what the values in your case can be, that is why i asked for more examples.
            With your single example it should be pretty easy if you have a "string" followed by a comparison or something and followed by a number.

            you could use a regex like "(\w+)(\W+)(\d+)" maybe?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              abich
              wrote on last edited by
              #6

              In fact i have several values such as:
              "x>=5 && y==0"
              "x>=5 || y==0"
              "x==5"
              "e=id,x=0"

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xander84
                wrote on last edited by
                #7

                well if you have AND (&&) and OR (||) relations and other stuff it might be a little more complicated, because you will have everything twice (variable, operator and value)?
                I was playing around a little this code might be helpful to you:
                @
                QString expression = "x>=5 && y==0";

                QRegExp xp(R"((\w+)(\W+)(\d+)\s*(&&|||)?)"); // c++11 raw string
                QString clockname, op, ter, con;
                int pos = 0;

                while ((pos = xp.indexIn(expression, pos)) != -1)
                {
                clockname = xp.cap(1);
                op = xp.cap(2);
                ter = xp.cap(3);
                con = xp.cap(4); // optional
                pos += xp.matchedLength();
                qDebug() << clockname << op << ter << con;
                }
                @
                output for this example:
                @
                "x" ">=" "5" "&&"
                "y" "==" "0" ""
                @
                if you can't use c++11 raw string you have to escape the regex (use \ for every )...

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

                  it doesn't work unfortunately :/
                  What's c++ 11 raw ?? i used the expression with \, it is not working too

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    abich
                    wrote on last edited by
                    #9

                    the expression i used was:
                    @QRegExp xp("((\w+)(\W+)(\d+)\s*(\&\&|\|\|)?)");@

                    output:
                    @"x>=5&&" "x" ">=" "5"
                    "y==0" "y" "==" "0"@

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      Xander84
                      wrote on last edited by
                      #10

                      yeah sorry for the confusion with the raw string...

                      a raw string looks like this
                      @
                      R"(string)" == "string"
                      @
                      the () are part of the string delemiter and not the string content I think that confused you, so to convert my raw string you have to omit the outer ():
                      @
                      QRegExp xp(R"((\w+)(\W+)(\d+)\s*(&&|||)?)"); // c++11 raw string
                      // same as
                      QRegExp xp("(\w+)(\W+)(\d+)\s*(\&\&|\|\|)?"); // escaped string
                      @
                      if you leave the outer "( )" that is obviously the first regex capture ground and not want you wanted I guess. :)

                      If you want to learn more about c++11 string literals take a look at this site for example: http://en.cppreference.com/w/cpp/language/string_literal

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        abich
                        wrote on last edited by
                        #11

                        thanks for your help, it works! :)

                        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