Nominate our 2022 Qt Champions!

[SOLVED] Usage of QRegExp

  • I am parsing the output of @vol@ command such that I only get the volume serial number i.e like qwer-12w2. But I get nothing, here's my code:

    @#include <QtCore/QCoreApplication>
    #include <QProcess>
    #include <QDebug>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    QProcess process;
    process.start("cmd /c vol C:");

    QByteArray out = process.readAllStandardOutput();
    qDebug() << out;
    QRegExp rx("The Volume Serial Number is (.+)\\.");
        QString id = rx.cap(1);
        qDebug() << id;
    return a.exec(&#41;;


  • Hi,

    Took me a few seconds to figure it out but here it is:
    On my computer the output of your process ("cmd /c vol C:") is (Windows 7):
    Volume in drive C is SYSTEM
    Volume Serial Number is 9C75-16CB

    If you look at your RexExp pattern you would notice that your patter starts with "The" while the output does not have a "The" in (first problem).

    My output does not contain a '.' at the end: Second Problem

    My suggestion is to use a patter like:
    QRegExp rx(".*Volume Serial Number is (.+)$");

    That should work for what you are trying to do

  • Got it :) Thank you @Badger.

Log in to reply