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开发者_如何学C 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);
}
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";
here you can scape your strings with this built-in function:
QRegExp::escape(QSTRING_HERE)
精彩评论