Results 1 to 17 of 17

Thread: qml and regexp; non-repeating digits and no leading zero

  1. #1
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml and regexp; non-repeating digits and no leading zero

    Quote Originally Posted by djvujke View Post
    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.

    Also i'm wondering why this reg exp. isn't working like its supposed to ? is it wrongly written?
    Apparently
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qml and regexp; non-repeating digits and no leading zero


    can you help me fix it?

  6. #6
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml and regexp; non-repeating digits and no leading zero

    No, QRegExp is not modelled on perl expressions. QRegularExpression is.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  11. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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:

    Qt Code:
    1. ^([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:
    1. 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.

  12. #12
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

  13. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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.

  14. #14
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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);

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qml and regexp; non-repeating digits and no leading zero

    QRegExp vs QRegularExpression. Read about differences between the two.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: qml and regexp; non-repeating digits and no leading zero

    Quote Originally Posted by djvujke View Post
    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.
    Last edited by jefftee; 19th March 2015 at 22:35.

  17. #17
    Join Date
    Apr 2006
    Posts
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Display more than four digits/characters
    By StarRocks in forum Newbie
    Replies: 8
    Last Post: 2nd February 2013, 09:12
  2. All digits of float to QString
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2012, 07:20
  3. auto-repeating arrows
    By skrzypu in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2008, 09:32
  4. setNum() digits question
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2008, 15:19
  5. coordinal significant digits
    By nitriles in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2007, 15:54

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.