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. QRegularExpressionValidator matching incorrectly lines with leading or trailing whitespace
Forum Updated to NodeBB v4.3 + New Features

QRegularExpressionValidator matching incorrectly lines with leading or trailing whitespace

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 501 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Snorkelbuckle
    wrote on last edited by Snorkelbuckle
    #1

    Can somebody check this regex pattern, is it correct? I think it is and I believe QRegularExpressionValidator may have a bug. So please, let me know if you think my regex or code is wrong and how to fix before I head over to the bug submission area. Thanks!

    Basically, I want to only allow input that doesn't have leading or trailing white space, white space in the middle is okay. Yes, I know I could just trim the text after it is entered, but I choose this way because I prefer to make input transparent, you get what you input unless the validators block you.

    UPDATE: Modified examples to match reality
    Examples of what is expected, in quotes:

     // Example 1: This is okay, no leading or trailing spaces
    "something"    
    
     // Example 2: Not okay, 1 or more leading space
    "    something else"   
    
    // Example 3: Not okay, 1 or more trailing space 
    "huh?   "
    
    QRegularExpression input_str_rx("^[A-Za-z0-9]+(?: +[A-Za-z0-9]+)*$");
    input_str_validator = new QRegularExpressionValidator(input_str_rx, this);
    ui->lineEdit_user->setValidator(input_str_validator);
    

    The problem is that the validator correctly allows Example 1 and it correctly prevents Example 2, but Example 3 it incorrectly allows. I tested the regex with two different regex testers and they both correctly handle the validation. One of them is a utility I've been using for years without any issue.

    Suprisingly, the second one that correctly handles the cases was compiled from the example project packaged with Qt Creator! It is the "Regular Expressions Example", but it is not using QRegularExpressionValidator, it just shows the results of the expressions themselves.

    I suppose I could make a custom validator, I just didn't want to go that route, I like to keep things simple.

    What do you all think?
    Thanks.

    aha_1980A 1 Reply Last reply
    0
    • S Snorkelbuckle

      Can somebody check this regex pattern, is it correct? I think it is and I believe QRegularExpressionValidator may have a bug. So please, let me know if you think my regex or code is wrong and how to fix before I head over to the bug submission area. Thanks!

      Basically, I want to only allow input that doesn't have leading or trailing white space, white space in the middle is okay. Yes, I know I could just trim the text after it is entered, but I choose this way because I prefer to make input transparent, you get what you input unless the validators block you.

      UPDATE: Modified examples to match reality
      Examples of what is expected, in quotes:

       // Example 1: This is okay, no leading or trailing spaces
      "something"    
      
       // Example 2: Not okay, 1 or more leading space
      "    something else"   
      
      // Example 3: Not okay, 1 or more trailing space 
      "huh?   "
      
      QRegularExpression input_str_rx("^[A-Za-z0-9]+(?: +[A-Za-z0-9]+)*$");
      input_str_validator = new QRegularExpressionValidator(input_str_rx, this);
      ui->lineEdit_user->setValidator(input_str_validator);
      

      The problem is that the validator correctly allows Example 1 and it correctly prevents Example 2, but Example 3 it incorrectly allows. I tested the regex with two different regex testers and they both correctly handle the validation. One of them is a utility I've been using for years without any issue.

      Suprisingly, the second one that correctly handles the cases was compiled from the example project packaged with Qt Creator! It is the "Regular Expressions Example", but it is not using QRegularExpressionValidator, it just shows the results of the expressions themselves.

      I suppose I could make a custom validator, I just didn't want to go that route, I like to keep things simple.

      What do you all think?
      Thanks.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Snorkelbuckle,

      if you call hasAcceptableInput() on your examples, the third one should return false, does it?

      I think the main problem with your regex is: you need to allow entering a space at the end to be able to enter further characters afterwards. if you forbid entering a space at the end, then you will not be able to enter example 1.

      Am I missing something?

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Snorkelbuckle
        wrote on last edited by Snorkelbuckle
        #3

        Hiya @aha_1980

        BTW, updated the examples above.

        Okay, silly me. This is really a logic error on my part and not fully understanding how the built-in validators work. Basically, they are validating in real-time as each character is entered. I assumed they were validated only after focus of the QLineEdit was lost or enter key is pressed, etc.

        So yes, I think you are right, need to allow entering space at the end to be able to enter further characters afterwards. :)

        It looks like I need to create a custom validator by subclassing QValidator and making use of QValidator::Intermediate in the validate() function. Does this sound right to you?

        Thanks.

        aha_1980A 1 Reply Last reply
        0
        • S Snorkelbuckle

          Hiya @aha_1980

          BTW, updated the examples above.

          Okay, silly me. This is really a logic error on my part and not fully understanding how the built-in validators work. Basically, they are validating in real-time as each character is entered. I assumed they were validated only after focus of the QLineEdit was lost or enter key is pressed, etc.

          So yes, I think you are right, need to allow entering space at the end to be able to enter further characters afterwards. :)

          It looks like I need to create a custom validator by subclassing QValidator and making use of QValidator::Intermediate in the validate() function. Does this sound right to you?

          Thanks.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Snorkelbuckle said in QRegularExpressionValidator matching incorrectly lines with leading or trailing whitespace:

          Okay, silly me. This is really a logic error on my part and not fully understanding how the built-in validators work

          I've gone that way too ;)

          It looks like I need to create a custom validator by subclassing QValidator and making use of QValidator::Intermediate in the validate() function. Does this sound right to you?

          For some problems that's indeed the only possible way.

          But before you start: what do you want to achive? If it's just disabling of a button, then you can use hasAcceptableInput() in a textChanged() slot. If you really want to forbid to just enter trailing spaces, then I'm honestly not sure how to achive that.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          3

          • Login

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