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] Help for easy QRegExp

[SOLVED] Help for easy QRegExp

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.4k 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.
  • R Offline
    R Offline
    rikytres
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      '.' 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
      0
      • R Offline
        R Offline
        rikytres
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rikytres
          wrote on last edited by
          #4

          YES! It's works! :D

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rikytres
            wrote on last edited by
            #5

            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
            0
            • D Offline
              D Offline
              DerManu
              wrote on last edited by
              #6

              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
              0
              • R Offline
                R Offline
                rikytres
                wrote on last edited by
                #7

                Ok! ;)

                Thanks a lot to everybody!

                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