QRegularExpressionValidator matching incorrectly lines with leading or trailing whitespace
-
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. -
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.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
-
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.
-
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.
@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 atextChanged()
slot. If you really want to forbid to just enter trailing spaces, then I'm honestly not sure how to achive that.Regards