Question

I have a text file which looks like this:

VariableA = 10 VariableB = 20 VariableC = "Hello World"

The code works fine, but my trouble is getting the text strings between " ".

QStringList Data;
Data << "VariableA = " << "VariableB = " << "VariableC = ";
QStringList Values;

int VariableA;
int VariableB;
QString VariableC;


foreach(const QString &DataToFind, Data) {
    QRegExp DataExpression(DataToFind);
    int DataStart = DataExpression.indexIn(TextToFind);
    if(DataStart >= 0) {
        int DataLength = DataExpression.matchedLength();
        int ValueSize = 1;
        while(TextToFind.at(DataStart + DataLength + ValueSize) != QChar(' ')) {
            ValueSize++;
        }
        QStringRef DataValue(&TextToFind, DataStart + DataLength, ValueSize);
        Values += DataValue.toString(); 
        DataStart = DataExpression.indexIn(description, DataStart + DataLength);
    } else {
        continue;
    }
}

VariableA = Values[0].toInt();
VariableB = Values[1].toInt();
VariableC = Values[2];

The issue is that the text on VariableC can have spaces and/or " (double quotes) inside it. So the method I've posted above to retrieve the variables from the file is useless. Since it uses " " to reach end of variable in the file.

How can I retrieve the full text inside the double quotes?

Was it helpful?

Solution

QStringList Data;
Data << "A = " << "B = " << "X = ";

int A;
int B;
QString X;

foreach(const QString &DataToFind, Data) {
    QRegExp DataExpression(DataToFind);
    int DataStart = DataExpression.indexIn(TextToFind);
    if(DataStart >= 0) {
        int DataLength = DataExpression.matchedLength();
        int ValueSize = 1;
        while(TextToFind.at(DataStart + DataLength + ValueSize) != QChar(' ')) {
            ValueSize++;
        }
        QStringRef DataValue(&TextToFind, DataStart + DataLength, ValueSize);
        DataStart = DataExpression.indexIn(description, DataStart + DataLength);
    } else {
        continue;
    }
}

This does the work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top