QLands
QLands

Reputation: 2586

Qt Regex check for special characters

I'm trying to check for special characters. I tried:

QString test;
test = "Hello";
QRegExp re("[^A-Za-z0-9]");
if (!re.exactMatch(test))
{
   log("False");
}

Which returns False

Also

int icount = test.count(QRegExp("[!@#$%^&()_+]"));

Which returns > 0

I don't know what am I doing wrong!

What I need is to know if a QString contains any other character than the valid: A-Z,a-z,0-9

Upvotes: 1

Views: 3263

Answers (1)

Maxim Paperno
Maxim Paperno

Reputation: 4869

Try QRegExp::indexIn()

QRegExp re("[^A-Za-z0-9]");
if (re.indexIn("Hello") < 0)
   qDebug() << "No special chars";
else
   qDebug() << "Found at least one special char";

if (re.indexIn("Hello.") < 0)
   qDebug() << "No special chars";
else
   qDebug() << "Found at least one special char";

Output:

No special chars
Found at least one special char

Upvotes: 1

Related Questions