can you help me fix it?
can you help me fix it?
when checking regexp (?!.*([0-9]).*\1)^([1-9][0-9]{3})$/gm on web page http://regexr.com/ it behaves as expected
but it doesn't work in qt validator
why?
examples tried on http://regexr.com/ , page will mark good with blue color.
012345
32654
12
1231
1021
1026
1022
1024
0234
1998
1983
1231
1235
1567
3656
3587
I cannot be certain but the validator instantiate QRegExpValidator which used QRegExpValidator which has less capabilities than the receipt you require.
after some searching -> link http://doc.qt.io/qt-5/qregexp.html
its seems that QRegExp is modeled on Perl's regexp language.
so who can write an perl's regexp for the case that i'm looking for![]()
No, QRegExp is not modelled on perl expressions. QRegularExpression is.
I went with following code:
* * *
// here i'vel limited lineEdit to 4 digit numbers that don't start with 0
QRegExp exp("^[1-9][0-9]{3}$");
ui->lineEdit->setValidator(new QRegExpValidator (exp, this));
* * *
//here i've checked for repeating digits in the number and signal the warning
void Widget::isValidGuess()
{
// Check for valid input,0 can't be in 1st place, no digit repetition
guess = ui->lineEdit->text();
// if number enteres has less than 4 digits display warning and disable guess button
if (ui->lineEdit->text().length() < 4){
ui->btnGuess->setEnabled(false);
ui->lblWarning->show();
ui->lineEdit->setFocus();
} else {
// if number enteres has 4 digits check for repetition
for (int i;i<4;i++){
if (guess.count(guess[i])> 1){
ui->btnGuess->setEnabled(false);
ui->lblWarning->show();
ui->lineEdit->setFocus();
break;
} else{
//no repetition found, enable guess button
ui->btnGuess->setEnabled(true);
}
}
}
}
I'm satisfied, so thread can be closed, but you are free to post any new solution you might have
thank you for your replays
A quick test of the following perl regular expression seems to work, at least in the minimal testing that I performed:
Qt Code:
^([1-9])(?!\1)([0-9])(?!\1|\2)([0-9])(?!\1|\2|\3)([0-9])$To copy to clipboard, switch view to plain text mode
I believe this would only work with QRegularExpression (not QRegExp) and don't forget to double up the \ when you use it inside a quoted string like:
Qt Code:
QRegularExpression regex("^([1-9])(?!\\1)([0-9])(?!\\1|\\2)([0-9])(?!\\1|\\2|\\3)([0-9])$");To copy to clipboard, switch view to plain text mode
Hope that helps.
Thanks for your answer.
I'd like to try your code but don't know how to implement it, as validator only accepts QRegExpValidator and not QRegularExpression.
Where did you test the regular expression, can you share the link?
I used the following site for testing the regex:
https://regex101.com/r/cC6fW3/1
I don't know QML, but I do see that there is a QRegularExpressionValidator class, so perhaps you can use that or roll your own custom validation function that uses QRegularExpression.
Hope that helps.
Thanks for you answer! this is the regular expression i was looking for. it passes my tests on the page you provided
now i'm trying to implement it
ui->setupUi(this);
QRegExp regex("^([1-9])(?!\\1)([0-9])(?!\\1|\\2)([0-9])(?!\\1|\\2|\\3)([0-9])$");
QValidator *validator = new QRegExpValidator(regex, this);
ui->lineEdit->setValidator(validator);
ui->lineEdit->setFocus();
but lineEdit only accepts first digit...do you maybe know why?
i followed the example that i've found in the documentation
// regexp: optional '-' followed by between 1 and 3 digits
QRegExp rx("-?\\d{1,3}");
QValidator *validator = new QRegExpValidator(rx, this);
QLineEdit *edit = new QLineEdit(this);
edit->setValidator(validator);
QRegExp vs QRegularExpression. Read about differences between the two.
QRegExp doesn't understand or fully implement Perl compatible regular expressions, so when it gets to the next negative look-ahead, it doesn't understand it.
You can stop trying to make the regex I gave you work with QRegExp as it simply won't work. If you are using Qt 5.1 or higher, use QRegularExpressionValidator instead of QRegExpValidator.
If you are still using Qt 4.x, then sorry, I don't have a regex for you that will accomplish what you want using QRegExp.
Edit: I do see where QRegExp supposedly supports pcre negative lookaheads (?!...) but it simply doesn't appear to work. It works fine with QRegularExpression though.
Last edited by jefftee; 19th March 2015 at 22:35.
Thanks
This is working code
QRegularExpression regex("^([1-9])(?!\\1)([0-9])(?!\\1|\\2)([0-9])(?!\\1|\\2|\\3)([0-9])$");
QValidator *validator = new QRegularExpressionValidator(regex, this);
ui->lineEdit->setValidator(validator);
case closed
Bookmarks