Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] Help for easy QRegExp

    General and Desktop
    3
    7
    5873
    Loading More Posts
    • 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.
    • R
      rikytres last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • T
        tobias.hunger last edited by

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

        1 Reply Last reply Reply Quote 0
        • R
          rikytres last edited by

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

          1 Reply Last reply Reply Quote 0
          • R
            rikytres last edited by

            YES! It's works! :D

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

            1 Reply Last reply Reply Quote 0
            • R
              rikytres last edited by

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

              1 Reply Last reply Reply Quote 0
              • D
                DerManu last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • R
                  rikytres last edited by

                  Ok! ;)

                  Thanks a lot to everybody!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post