devdon
22nd October 2011, 16: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;
}
/// 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;
}