Will
Will

Reputation: 99

Qt 4.7 QRegExp Email Address Validation

I have a good regular expression pattern for validating email addresses. I've used it in php and I've used it in C#, not come across any problems (none yet). I am hwoever having considerable trouble migrating the pattern and using it with qt's QRegExp.

Can anyone help me?

// C# version
public bool isEmailAddress(string strEmailAddr)
{
    if (strEmailAddr.Length == 0)
        return false;

    Regex rTest = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase);
    return rTest.Match(strEmailAddr).Success;
}

// #C++/Qt 4.7 version ... not working
bool isEmailAddress(QString strEmailAddr)
{
    if ( strEmailAddr.length() == 0 ) return false;

    QString strPatt = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\b";
    QRegExp rx(strPatt);
    return rx.exactMatch(strEmailAddr);
}

Upvotes: 3

Views: 4773

Answers (2)

Fernando Lopez Jr.
Fernando Lopez Jr.

Reputation: 21

here you can scape your strings with this built-in function:

QRegExp::escape(QSTRING_HERE)

Upvotes: 2

Kaleb Pederson
Kaleb Pederson

Reputation: 46479

C#'s raw string made it a bit easier to write but since you're dealing with C++, you need to escape the backslashes:

QString strPatt = "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b";

Upvotes: 7

Related Questions