user931018
user931018

Reputation: 703

(Qt) Validate string against multiple regular expressions simultaneously

I'm checking a string which contains vehicle registration information against regular expressions for validity. I have several regular expression for each criteria I need. How can I validate the string against all my reg expressions without having to combine them into one expression or do something like this to determine if it's valid?

if( s_expGP.exactMatch(lineEdit->text()) ||
    s_expGPNew.exactMatch(lineEdit->text()) ||
    s_expPersonal.exactMatch(lineEdit->text()) ||
    s_expGov.exactMatch(lineEdit->text()) )
{
    //do stuff
}

Upvotes: 0

Views: 617

Answers (2)

ymoreau
ymoreau

Reputation: 3996

If you have a big number of regexp to test or if you may need to verify the string more than once. You can create a function like this

bool isValid(const QVector<QRegExp>& regExps, const QString& input)
{
    for(QRegExp exp : regExps)
    {
        if(!exp.exactMatch(input))
            return false;
    }
    return true;
}

Or use a static QVector like you have static regexp.

Upvotes: 0

Naumann
Naumann

Reputation: 407

The only option would be to create a single regular expression by combining s_expGP, s_expGPNew, s_expPersonal and the rest if that is possible, otherwise I don't think there could be any other way.

Upvotes: 1

Related Questions