如何使用qt获取复杂的子字符串?

时间:2016-07-08 04:45:45

标签: c++ qt qregularexpression

我正在研究QT和c ++。作为测试项目的一部分,我需要从复杂的长字符串中获取子字符串,基本上它是网页的源。如果我的问题看起来非常微不足道,那就是因为我是一个天真的qt程序员,并且对正则表达式的经验较少。

字符串: “url_encoded_fmt_stream_map”:“类型=视频%2Fwebm%3B +编解码器的%3D%22vp8.0%2C + Vorbis的%22 \ u0026quality =介质\ u0026itag = 43 \ u0026url = HTTPS%3A%2F%2Fr2 --- sn-ci5gup-qxaz.googlevideo.com%2Fvideoplayback%3Fmime%3Dvideo%252Fwebm%26key%3Dyt6%26initcwndbps%3D386250%26pcm2cms%3Dyes%26requiressl%3Dyes%26signature%3D05FB41F2F9FF5490841C7B9F40748FC4B8F505A3.0D81AB0745519F179CBF4468D2412C2240DC72F0%26ratebypass%3Dyes%26fexp%3D3300119%252C3300131% 252C3313268%252C9405961%252C9416126%252C9416891%252C9419451%252C9422342%252C9422596%252C9427790%252C9428398%252C9431012%252C9433096%252C9433380%252C9433855%252C9433946%252C9434332%252C9435526%252C9435667%252C9435876%252C9436270%252C9436607%252C9436834%252C9436872%252C9436973%252C9437066%252C9437553% 252C9437742%252C9438326%252C9439652%252C9439979%252C9440048%252C9440552%252C9440651%26ms%3Dau%26mt%3D1467900993%26sparams%3Ddur%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms% 252Cpl%252Cratebypass%252Creq uiressl%252Csource%252Cupn%252Cexpire%26mv%3DM%26ip%3D182.68.203.45%26id%3DO-AMB0Fggb71vKhXSdQqcOPdLeb9v0n2GaibsWg_W5L5b7%26毫米%3D31%26mn%3Dsn-ci5gup-qxaz%26upn%3Dfri9F8GP9uA%26source%3Dyoutube%26itag%3D43% 26pl%3D20%26lmt%3D1445550849945309%26expire%3D1467922869%26sver%3D3%26ipbits%3D0%26dur%3D0.000,类型=视频%2Fmp4%3B +编解码器的%3D%22avc1.42001E%2C + mp4a.40.2%22 < /strong>\u0026quality=medium\u0026itag=18\u0026url=https%3A%2F%2Fr2---sn-ci5gup-qxaz.googlevideo.com%2Fvideoplayback%3Fmime%3Dvideo%252Fmp4%26key%3Dyt6%26initcwndbps %3D386250%26pcm2cms%3Dyes%26requiressl%3Dyes%26signature%3D4C90C634F68098572BDCA7D5B7946F8A49A17369.D4AB7172922F499596226BA335BDF0B4519A4AC6%26ratebypass%3Dyes%26fexp%3D3300119%252C3300131%252C3313268%252C9405961%252C9416126%252C9416891%252C9419451%252C9422342%252C9422596%252C9427790%252C9428398%252C9431012%252C9433096%252C9433380 %252C9433855%252C9433946%252C9434332%252C9435526%252C9435667%252C9435876%252C9436270%252C9436607%252C9436834%252C9436872%252C9436973%25 2C9437066%252C9437553%252C9437742%252C9438326%252C9439652%252C9439979%252C9440048%252C9440552%252C9440651%26ms%3Dau%26mt%3D1467900993%26sparams%3Ddur%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt%252Cmime%252Cmm%252Cmn%252Cms% 252Cmv%252Cpcm2cms%252Cpl%252Cratebypass%252Crequiressl%252Csource%252Cupn%252Cexpire%26mv%3DM%26ip%3D182.68.203.45%26id%3DO-AMB0Fggb71vKhXSdQqcOPdLeb9v0n2GaibsWg_W5L5b7%26毫米%3D31%26mn%3Dsn-ci5gup-qxaz%26upn%3Dfri9F8GP9uA% 26source%3Dyoutube%26itag%3D18%26pl%3D20%26lmt%3D1465194153116908%26expire%3D1467922869%26sver%3D3%26ipbits%3D0%26dur%3D207.354,类型=视频%2F3gpp%3B +编解码器的%3D%22mp4v.20.3% 2C + mp4a.40.2%22 \ u0026quality =小\ u0026itag = 36 \ u0026url =

上面的字符串是我完整字符串和类似模式的重要部分。这个网页源有许多编码的网址,我需要分开。以下是我要分开的内容。

子串:

HTTPS%3A%2F%2Fr2 --- sn-ci5gup-qxaz.googlevideo.com%2Fvideoplayback%3Fmime%3Dvideo%252Fwebm%26key%3Dyt6%26initcwndbps%3D386250%26pcm2cms%3Dyes%26requiressl%3Dyes%26signature%3D05FB41F2F9FF5490841C7B9F40748FC4B8F505A3 .0D81AB0745519F179CBF4468D2412C2240DC72F0%26ratebypass%3Dyes%26fexp%3D3300119%252C3300131%252C3313268%252C9405961%252C9416126%252C9416891%252C9419451%252C9422342%252C9422596%252C9427790%252C9428398%252C9431012%252C9433096%252C9433380%252C9433855%252C9433946%252C9434332%252C9435526%252C9435667%252C9435876%252C9436270 %252C9436607%252C9436834%252C9436872%252C9436973%252C9437066%252C9437553%252C9437742%252C9438326%252C9439652%252C9439979%252C9440048%252C9440552%252C9440651%26ms%3Dau%26mt%3D1467900993%26sparams%3Ddur%252Cid%252Cinitcwndbps%252Cip%252Cipbits%252Citag%252Clmt %252Cmime%252Cmm%252Cmn%252Cms%252Cmv%252Cpcm2cms%252Cpl%252Cratebypass%252Crequiressl%252Csource%252Cupn%252Cexpire%26mv%3DM%26ip%3D182.68.203.45%26id%3DO-AMB0Fggb71vKhXSdQqcOPdLeb9v0n2GaibsWg_W5L5b7%26毫米%3D 31%26mn%3Dsn-ci5gup-qxaz%26upn%3Dfri9F8GP9uA%26source%3Dyoutube%26itag%3D43%26pl%3D20%26lmt%3D1445550849945309%26expire%3D1467922869%26sver%3D3%26ipbits%3D0%26dur%3D0.000,类型=视频%2Fmp4%3B +编解码器的%3D%22avc1.42001E%2C + mp4a.40.2%22

请帮助并建议我如何使用QT,C ++,正则表达式来做到这一点。我不想使用任何其他编程语言。 谢谢&amp;问候。

1 个答案:

答案 0 :(得分:0)

在Qt中使用QString类用于处理字符串。您应该学习QString文档部分:Manipulating String DataQuerying String Data。也许QString类的contains()方法足以解决您的问题。

<强> UPD:

子串操作的简单示例:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString bigString = "BigString BigString SubString BigString";
    QString subString = "SubString";

    bool containsFlag = bigString.contains(subString, Qt::CaseSensitive);
    int index = bigString.indexOf(subString, 0, Qt::CaseSensitive);

    qDebug() << "Length of subString: " << subString.length();
    qDebug() << "SubString contains in bigString: " << containsFlag;
    qDebug() << "SubString index in bigString: " << index;

    return a.exec();
}

如您所见,您可以确定subString中是否包含bigString。当然,您可以在subString中确定bigString的索引。 您可以将自己的数据放在bigStringsubString