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. Not getting what I expected using a regex in QRegExpValidator.
QtWS25 Last Chance

Not getting what I expected using a regex in QRegExpValidator.

Scheduled Pinned Locked Moved General and Desktop
qvalidatorregexqregexpvalidato
6 Posts 3 Posters 2.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.
  • E Offline
    E Offline
    ealione
    wrote on 12 Jun 2015, 09:15 last edited by
    #1

    I am using a QLineEdit with a QRegExpValidator in order to limit inputs to this type:

    a.green or d.yellow4 and so on.

    So basically one lowercase character followed by a period and then an arbitrary number of characters and possibly a number at the end.

    What I have are the following

    QRegExp textValidator("^[a-z]+\.?[a-z]+[0-9]*$"); 
    QRegExpValidator *validator = new QRegExpValidator(textValidator, 0);
    

    and

    QRegExp textValidator("[a-z]\.\w++"); 
    QRegExpValidator *validator = new QRegExpValidator(textValidator, 0);
    

    With the first one allowing pretty much anything and the second one pretty much nothing (I'm a man of the extremes what can I say).

    Can someone help me fix my validator?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leonardo
      wrote on 12 Jun 2015, 13:46 last edited by
      #2

      Well, the first problem I see here is the escaping. The backslash is being consumed by the C++ compiler and doesn't get to the regex parser. You need to double escape it, to compensate this issue. Like this:

      "^[a-z]+\\.?[a-z]+[0-9]*$"
      

      You said the string can only have one lowercase character, so you need to remove the first +. If both the period and the following characters are optional, they should be grouped.

      "^[a-z](\\.[a-z]+)?[0-9]*$"
      
      1 Reply Last reply
      1
      • E Offline
        E Offline
        ealione
        wrote on 12 Jun 2015, 13:52 last edited by
        #3

        @Leonardo said:

        "^a-z?[0-9]*$"

        Thanks for the quick reply. I can confirm it is working as intended.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Jun 2015, 21:38 last edited by
          #4

          Hi,

          You can check the regularexpression example (available starting 5.5). It's a little application that helps test and build regexp to use with QRegularExpression

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ealione
            wrote on 15 Jun 2015, 08:54 last edited by
            #5

            5.5 you say. Interesting :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Jun 2015, 09:07 last edited by
              #6

              I do :)

              However, you don't need to build Qt 5.5 to build it, you can use your current Qt version

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              12 Jun 2015, 09:15

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved