如何从字符串中提取相关内容

实例1 sql语句相关

    {
        /**
         * \s       表示一个\t、一个\n等
         * \s       Matches a whitespace character (QChar::isSpace()).
         * \s*      多个
         * \s+      至少一个
         * \s{2}    两个
         * \s{1,2}  一个到两个
         * \w       表示一个字母、一个数字、一个汉字等
         * \w       Matches a word character (QChar::isLetterOrNumber(), QChar::isMark(), or '_').
         * \w*      多个
         * \w+      至少一个
         * \w{2}    两个
         * \w{1,2}  一个到两个
         * ()       表示提取的位置
         */
        QString Output = "select obid from cey_bt_table where ch_billno = 'Q001' ";
        QRegExp rx(R"(select\s*(\w*)\s*from\s*(\w*)\s*where\s*(\w*)\s*=\s*'(\w*)'\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);
        }
        return 1;
    }

运行结果:

Da Data Thing: 0 Da Value Thing: "select obid from cey_bt_table where ch_billno = 'Q001' "

Da Data Thing: 1 Da Value Thing: "obid"

Da Data Thing: 2 Da Value Thing: "cey_bt_table"

Da Data Thing: 3 Da Value Thing: "ch_billno"

Da Data Thing: 4 Da Value Thing: "Q001"

实例2 mac地址相关

{
        /**
         * \d  表示一个数字
         * \d  Matches a digit (QChar::isDigit()).
         * \d+ 表示重复一次或者多次 1、12、23
         * \d* 表示重复零次或者多次 _、12、11
         * \d{2} 表示只能匹配两个字符
         */
        QString Output;
        Output="Duration: 00:01:25.65, start: 0.050000, bitrate: 5200 kb/s";
        QRegExp rx = QRegExp(R"(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);
        }
    }

运行结果:

Da Data Thing: 0 Da Value Thing: "Duration: 00:01:25.65, start: 0.050000, bitrate: 5200 kb/s"

Da Data Thing: 1 Da Value Thing: "00:01:25.65"

Da Data Thing: 2 Da Value Thing: "0.050000"

Da Data Thing: 3 Da Value Thing: "5200"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值