Important: Please read the Qt Code of Conduct -

[solved] Parsing expressions in xml file

  • 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?

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

  • 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:

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

    PS: i have Qt 4

  • 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?

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

  • 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 )...

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

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

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

  • 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:

  • thanks for your help, it works! :)

Log in to reply