[SOLVED] Help for easy QRegExp



  • Hi guys!
    I need some help with a regular expression.

    I have a QLineEdit that take an input QString, this QString can be "(float, float)" or "((float, float)(float, float))" or "((float, float)(float, float)(float, float))"...
    This input represent a point, a line and an triangle.

    I connect my QLineEdit to a slot that test, on textChange, if there one of the precedent object inside its.
    I create a QRegExp for any type of input.
    And anything works if i find the correct regular expression for matching a float number...

    Than, now, the problem is find a float number....
    I try with this:
    @QRegExp float("[0-9]+.?[0-9]*");@
    But this is not correct, because the engine doesn't understand that i want only a "." and not any letter...

    For exemple...
    any of this input match with the regular expression: "1.1", "1 1", "1a1".

    PS: I have to use the QRegExp::exactMatch() function.


  • Moderators

    '.' is "any character" in regexp.

    You need to escape this to "." for the regexp to match a dot. Since the string is escaped before it is passed on to QRegExp you need to escape that escape sequence once again. So in effect you need to put "\." into your string.

    You can also replace "[0-9]" with "\d" (\d matches any diget).



  • Good!
    Well, now I have this:
    @QRegExp float("\d+\.?\d*");@
    right?



  • YES! It's works! :D

    One more question... ;)
    If i have to match "(" i have to type this "\("?



  • Ok, sorry for the stupid question... I try it and it works...

    Perfect. This question is solved.
    Do i need to update the post's name adding [SOLVED]?



  • Just a remark: Usually you should match floating point values with the "standard"-pattern:
    @[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?@
    Make sure to split your regex in multiple lines of code to make it readable and commentable. Or in this case, with four floats, maybe even put that float-pattern into an own QString and build the final regex with it.

    About the [SOLVED] thing: yep.



  • Ok! ;)

    Thanks a lot to everybody!


Log in to reply
 

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