PDA

View Full Version : Calling QRegExp pattern from a variable gets -1



devdon
22nd October 2011, 17:31
Here's the code from a music program I'm writing. The regexp works. If I copy and paste it in (removing only the two extra \\ slashed for \\\\d*) it works great. But no matter what I do, and I've tried every combination I can think of or look up, pos returns -1. I'm stumped. Why won't this work when called from a variable? It is just text, right? I've tried various quoting methods, various escape sequences, etc. but no luck. Here's the code. Once again, it works when copied and pasted but not when sent as an escaped OR unescaped variable. Thank you for your help.



/// excerpt declaring the variable
else if ( key == "Ab Major") {
k->keyRegExp = (QString("^([b|e|a|d])(\\\\d*)"));
k->keyAccidental = "b";
k->kNum = 4;
k->kType = "Flats";

/// routine calling the variable and the regexp
KeySignature* ks = new KeySignature;
ks = ks->getCurrKeySigData();
QString key = ks->key;
QString keyRegExp = ks->keyRegExp;
QString acc = ks->keyAccidental;
QString nn = iNote->noteName;
QList<QString>resultList;
QRegExp kNotes(keyRegExp);
bool ok = kNotes.isValid();
int pos = kNotes.indexIn(nn);
QString pat = kNotes.pattern();
if (pos != -1) {
resultList = kNotes.capturedTexts();
nn = resultList.value(1) + acc + resultList.value(2);
stop++;

}

iNote->noteName = nn;
return iNote;
}

amleto
22nd October 2011, 17:51
how do you know it works if yo havent tested it with code/variables? What do you mean by copy/paste - copy what to where.

Why dont you just set up a small unit test for regex?

devdon
22nd October 2011, 18:03
This code is from the program. I've run it successfully using the regexp from the excerpt, copying and pasting the excerpt into the space where the keyRegExp variable is. But when I use the variable instead of the copy/pasted literal value the QString pat = kNotes.pattern(); shows that the regexp pattern is correct, the bool ok = kNotes.isValid(); shows that the pattern is valid, but int pos = kNotes.indexIn(nn); ALWAYS returns -1.

That's the problem.

norobro
22nd October 2011, 21:16
From the docs:
To include a \ in a regexp, enter it twice, i.e. \\. To match the backslash character itself, enter it four times, i.e. \\\\ So wouldn't your regexp be looking for b,e,a or d, followed by a "\", followed by zero or more occurances of "d"?

Does it not work with two backslashes?
QString("^([b|e|a|d])(\\d*)");

devdon
22nd October 2011, 21:46
Sorry, should have included that at first. It is looking for a note name followed by a number, such as e2. It will then take it apart and reassemble it as eb2. When I use a single \d in qt I get an "unknown token" warning. I have to use two \\d to make it work. I'm used to one \d in perl.

Added after 4 minutes:

If I use two slashes (unescaped) it gets translated to one and that won't work. I need to escape the slashed for it to end up in one piece at the other end. This is why this is so frustrating. The patten is ending up at the other end in one piece and is declared valid by the routine but doesn't ever match the simple target. But it does work if it doesn't come from the variable. Is there some trick to executing it from the variable?

Added after 20 minutes:

OK -- I have played with the slashed and got it to work. I had too many in operation at once and once I reduced their number everything worked ok. It only takes one slash just like in perl. I wonder why I though differently? I don't know. Thank you for your very useful help.