[Solved] QRegExp for a repeated pattern?



  • Does QRegExp support matching of repeated patterns? Let's consider a sequence of integers (some of which could be negative), separated by either a comma or a space. For example, 4,6,-28,10.

    The QRegExp for an optional minus, 1+ digits and a separator (comma or space) would be

    QRegExp pattern("-?[0-9]+[,\\s]");
    

    So I'd expect

    QRegExp repeatedPattern("[-?[0-9]+[,\\s]]*");
    

    to be the version for the repeated pattern described above. I know nested regular expressions can be ambiguous, but in this case it is not.

    To provide some context — I'm trying to constrain the input for a QLineEdit, using

    ui->lineEdit->setValidator(new QRegExpValidator(pattern));

  • Lifetime Qt Champion

    Hi,

    Unless you're using Qt 4, you should change to QRegularExpression then:

    ui->lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("(-?\\d+[,\\s])+")));
    

    should do what you want.

    Hope it helps



  • Excellent! Thanks.


  • Lifetime Qt Champion

    You're welcome !

    By the way, you don't have to edit your thread title anymore to mark the thread as solved, juts use the "Topic Tool" button.

    Also, while browsing the forum, please consider up-voting answers that helped you. It will make them easier to find for other forum users :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.