Frage

I'm having trouble splitting a QString properly. Unless I'm mistaken, for multiple delimiters I need a regex, and I can't seem to figure out an expression as I'm quite new to them.

the string is text input from a file:

f 523/845/1 524/846/2 562/847/3 564/848/4

I need each number seperately to put into an array.

Some codes....

QStringList x;
QString line = in.readLine();
        while (!line.isNull()) {
            QRegExp sep("\\s*/*");

            x =  line.split(sep);

Any pointers?

Cheers

War es hilfreich?

Lösung

Change your regular expression like this:

QRegExp sep("(\\s+|/)");

then x will have every number.

Andere Tipps

I found it quite useful to try out RegEx's interactively. Nowadays there are a lot of online tools even, for example: http://gskinner.com/RegExr/

You can put your search text there and play with the RegEx to see what is matched when.

You could use the strtok function, which split a QString with one or more different tokens.

It would be like this:

    QString a = "f 523/845/1 524/846/2 562/847/3 564/848/4";
    QByteArray ba = a.toLocal8Bit();
    char *myString = ba.data();
    char *p = strtok(myString, " /");

    while (p) {
        qDebug() << "p : " << p;
        p = strtok(NULL, " /");
    }

You can set as many tokens as you need. For further info visit the cplusplus page of this particular function. http://www.cplusplus.com/reference/cstring/strtok/

Regards!.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top