Mlarnt90
Mlarnt90

Reputation: 186

how to get sub string value from a string in QT C++ using regular expression

I have a string, such as:

QString Output;

Output="Duration: 00:01:25.65, start: 0.050000, bitrate: 2709 kb/s"`;

My question is how can I obtain those duration, start, and bitrate values separately.
For example, I need to get bitrate value, so how can I get the bitrate value in c++ using a regular expression.

Note that I don't have good knowledge about regular expressions.

Upvotes: 0

Views: 2973

Answers (2)

Sebastian Lange
Sebastian Lange

Reputation: 4029

You should at least try to read into QRegExp. There are some examples. Using reg expressions you can use capture groups QRegExp("Duration:\\s*(\\d{2}:\\d{2}:\\d{2}.\\d{2})") which will for example match your Duration time or QRegExp("start:\\s*(\\d+\\.\\d+),") for having start in capture group. If the string is always having this setup its easy to filter.

Now its up to you to find the bitrate

Edit:

QString Output;
Output="Duration: 00:01:25.65, start: 0.050000, bitrate: 2709 kb/s";
QRegExp rx = QRegExp("Duration:\\s*(\\d{2}:\\d{2}:\\d{2}.\\d{2}),\\s*start:\\s*(\\d+\\.\\d+),\\s*bitrate:\\s*(\\d*)\\s*kb/s");

rx.indexIn(Output);
QStringList qsl = rx.capturedTexts();

for(int i=0; i<qsl.count(); i++)
{
    qDebug()<<"Da Data Thing: "<< i << " Da Value Thing: " << qsl.at(i);
}

qsl.at(1) should be duration.

qsl.at(2) should be start.

qsl.at(3) should be rate.

Upvotes: 0

vahancho
vahancho

Reputation: 21248

And just in case - solution without using regular expressions:

enum Data {
    Duration,
    Start,
    Bitrate
};

int main(int argc, char *argv[])
{
    [..]
    QString str("Duration: 00:01:25.65, start: 0.050000, bitrate: 2709 kb/s");

    QStringList tokens = str.split(',');
    QString duration = tokens[Duration].split(' ', QString::SkipEmptyParts)[1];
    QString start = tokens[Start].split(' ', QString::SkipEmptyParts)[1];
    QString bitrate = tokens[Bitrate].split(' ', QString::SkipEmptyParts)[1];
    [..]

Upvotes: 1

Related Questions