PDA

View Full Version : QRegExp not reading literal character '-'



jshafferman
10th April 2012, 21:00
I am having problems getting a QRegExp to read the character '-'. I have a program that reads in QString and that string has to be in a specific order for the program to be able to label it correclty. Here is what I have so far:

QString itemReadIn = "SFPP-----------";

QRegExp exp("S?F?------?????");
exp.setPatternSyntax(QRegExp::Wildcard);

if(exp.exactMatch(itemReadIn))
{
// label item correctly
return;
}

So my probelm is that the if statement is never entered into so the labeling never happens correctly. I thought my expression was correct but I haven't been able to figure out why it won't work. I have also trie \\- for - and that doesn't seem to do anything to help :(. It is my understanding that the ? means any character which is exactly what I want and the '-' should be literally that character nothing else.

Any help would be greatly appreciated. Thanks!!

FelicianoX
10th April 2012, 22:42
Your wildcard match is trying to match the letter S, followed by any character, followed by a letter F. but in your match, the letter F is right after S

I think you want S?P?

Sorry for my bad English, hope you understood what I meant.

norobro
10th April 2012, 22:44
I suppose you meant "SF??------?????" in your regexp. If so my box spits out a warning "warning: trigraph ??- converted to ~ [-Wtrigraphs]"

I had to google trigraphs. :)

Try escaping one of the "?" or the first "-": "SF\??------?????"

jshafferman
10th April 2012, 23:00
I actually have S?P?------????? in my code but wrote it down wrong on here, but to norobro ~ I will try escaping it to see if that helps.

norobro
10th April 2012, 23:15
Works fine here with "S?P?------?????":
#include <QtGui>

int main(){
QString itemReadIn = "SFPP-----------";
QRegExp exp("S?P?------?????");
exp.setPatternSyntax(QRegExp::Wildcard);
if(exp.exactMatch(itemReadIn)) qDebug() << "match";
else qDebug() << "no match";
}

jshafferman
17th April 2012, 15:53
Grrr I must have done something wrong with my application... Can using a QString that is appended by chunks effect the program?

e.g. QString itemReadIn;
itemReadIn.append("S?P?);
itemReadIn.append("------?????");