-1

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

字符串: "url_encoded_fmt_stream_map":"type=video%2Fwebm%3B+codecs%3D%22vp8.0%2C+vorbis%22\u0026quality=medium\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%3D05FB41F2F904FF5448FC48484C0D81AB0745519F179CBF4468D2412C2240DC72F0%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%26mm%3D31%26mn%3Dsn-ci5gup-qxaz%26upn%3Dfri9F8GP9uA%26source%3Dyoutube%26itag%3D43%26pl%3D20%26lmt%3D1445550849945309%26expire%3D1467922869%26sver%3D3%26ipbits% 3D0%26dur%3D0.000,type=video%2Fmp4%3B+codecs%3D%22avc1.42001E%2C+mp4a.40.2%22\u0026质量=中等\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%3D4C90C634A1736804965872A17376F68BDA4D4AB7172922F499596226BA335BDF0B4519A4AC6%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%26mm%3D31%26mn%3Dsn-ci5gup-qxaz%26upn%3Dfri9F8GP9uA%26source%3Dyoutube%26itag%3D18%26pl%3D20%26lmt%3D1465194153116908%26expire%3D1467922869%26sver%3D3%26ipbits% 3D0%26dur%3D207.354,type=video%2F3gpp%3B+codecs%3D%22mp4v.20.3%2C+mp4a.40.2%22\u0026quality=small\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%3D05FB41F2F904FF5448FC48484C0D81AB0745519F179CBF4468D2412C2240DC72F0%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%26mm%3D31%26mn%3Dsn-ci5gup-qxaz%26upn%3Dfri9F8GP9uA%26source%3Dyoutube%26itag%3D43%26pl%3D20%26lmt%3D1445550849945309%26expire%3D1467922869%26sver%3D3%26ipbits% 3D0%26dur%3D0.000,type=video%2Fmp4%3B+codecs%3D%22avc1.42001E%2C+mp4a.40.2%22

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

4

1 回答 1

0

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

升级版:

子字符串操作的简单示例:

#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. 当然,您可以确定subStringin 的索引bigString。您可以将自己的数据放入bigString和中subString

于 2016-07-08T07:46:38.033 回答