qml and regexp; non-repeating digits and no leading zero
Hi to all,
i'm trying to make some app with qml and regular expressions
TextInput {
id: textInput1
validator: RegExpValidator {
regExp: /(?!.*([0-9]).*\1)^([1-9][0-9]{3})?/gm
}
there is textInput where you should put only digits, 4 of them, and there can't be digit repetition (no 2 same digits) and 0 can't be in first place.
when i test my regexp in http://regexr.com/ i see that it seems to be good, but it doesn't work as expected in qml
example of number that user can enter and i'll mark good ones.
0124 -bad , it has leading 0
1234 - good
1231 - bad it has 1 twice ---> but this one seems to passes the regExp validator mentioned previously
123 - bad its short, it has to be 4 digits
32654 - bad, it has more than 4 digits
where am i doing wrong?
Or do i have to do it some other way?
thanks for helping
Re: qml and regexp; non-repeating digits and no leading zero
You cannot do that with a regular expression validator without an insanely long validation string. Use a custom validator instead.
Re: qml and regexp; non-repeating digits and no leading zero
Hi, thanks for your replay.
When you say custom validator you think of making the function for that, right?
I'd like it to be done regexp as it would make the whole app simpler.
Also i'm wondering why this reg exp. isn't working like its supposed to ? is it wrongly written?
Re: qml and regexp; non-repeating digits and no leading zero
Quote:
Originally Posted by
djvujke
When you say custom validator you think of making the function for that, right?
More likely a Validator subclass but I guess a function would do as well.
Quote:
Also i'm wondering why this reg exp. isn't working like its supposed to ? is it wrongly written?
Apparently :)
Re: qml and regexp; non-repeating digits and no leading zero
:)
can you help me fix it?
Re: qml and regexp; non-repeating digits and no leading zero
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
Re: qml and regexp; non-repeating digits and no leading zero
I cannot be certain but the validator instantiate QRegExpValidator which used QRegExpValidator which has less capabilities than the receipt you require.
Re: qml and regexp; non-repeating digits and no leading zero
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 :)
Re: qml and regexp; non-repeating digits and no leading zero
No, QRegExp is not modelled on perl expressions. QRegularExpression is.
Re: qml and regexp; non-repeating digits and no leading zero
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
Re: qml and regexp; non-repeating digits and no leading zero
A quick test of the following perl regular expression seems to work, at least in the minimal testing that I performed:
Code:
^([1-9])(?!\1)([0-9])(?!\1|\2)([0-9])(?!\1|\2|\3)([0-9])$
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:
Code:
QRegularExpression regex("^([1-9])(?!\\1)([0-9])(?!\\1|\\2)([0-9])(?!\\1|\\2|\\3)([0-9])$");
Hope that helps.
Re: qml and regexp; non-repeating digits and no leading zero
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?
Re: qml and regexp; non-repeating digits and no leading zero
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.
Re: qml and regexp; non-repeating digits and no leading zero
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);
Re: qml and regexp; non-repeating digits and no leading zero
QRegExp vs QRegularExpression. Read about differences between the two.
Re: qml and regexp; non-repeating digits and no leading zero
Quote:
Originally Posted by
djvujke
but lineEdit only accepts first digit...do you maybe know why?
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.
Re: qml and regexp; non-repeating digits and no leading zero
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