Qt正则表达式提取数据

这几天在上嵌入式课程设计,需要用到Qt,这个是信号与槽的,寒假的时候也简单学习了一些,但是没有怎么深入,又回过来看了看Qt,发现Qt的ui界面配置与Android的好像,当然Qt也可以拿来开发Android。

废话不多说了,直接上代码:

用正则表达式提取数据                                                                       

复制代码
void testRegexCapture()
{
    QString pattern(“(.*)=(.*)”);
    QRegExp rx(pattern);

    QString str(“a=100″);
    int pos = str.indexOf(rx);              // 0, position of the first match.
                                            // Returns -1 if str is not found.
                                            // You can also use rx.indexIn(str);
    qDebug() << pos;
    if ( pos >= 0 )
    {
        qDebug() << rx.matchedLength();     // 5, length of the last matched string
                                            // or -1 if there was no match
        qDebug() << rx.capturedTexts();     // QStringList(“a=100″, ”a”, ”100″),
                                            //   0: text matching pattern
                                            //   1: text captured by the 1st ()
                                            //   2: text captured by the 2nd ()

        qDebug() << rx.cap(0);              // a=100, text matching pattern
        qDebug() << rx.cap(1);              // a, text captured by the nth ()
        qDebug() << rx.cap(2);              // 100,

        qDebug() << rx.pos(0);              // 0, position of the nth captured text
        qDebug() << rx.pos(1);              // 0
        qDebug() << rx.pos(2);              // 2
    }
}
复制代码

用正则表达式修改文本                                                                      

QString s = ”a=100″;
s.replace(QRegExp(“(.*)=”), ”b=”);
qDebug() << s;                          // b=100
QString s = ”a=100″;
s.replace(QRegExp(“(.*)=(.*)”), ”\\1\\2=\\2″);  // \1 is rx.cap(1), \2 is rx.cap(2)
qDebug() << s;                                  // a100=100

用正则表达式验证文本有效性                                                               

复制代码
void testRegexMatch()
{
    QString pattern(“.*=.*”);
    QRegExp rx(pattern);

    bool match = rx.exactMatch(“a=3″);
    qDebug() << match;                      // True

    match = rx.exactMatch(“a/2″);
    qDebug() << match;                      // False
}
复制代码

我是天王盖地虎的分割线                                                                   

当初了解到Qt是在学习MFC的时候,当时写MFC程序才叫伤心啊。

Qt一次编写,多次编译。在windows还是linux平台上,只需要换一下编译器就OK,代码都不需要怎么改的。

通过写linux上的程序,我涉及到的这个程序是arm板子上,通过串口通信读取zigbee发来的信息。主要是板子上驱动写的好,Qt写个串口通信就可以直接拿来用。

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut

posted @   我爱物联网  阅读(8637)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
AmazingCounters.com
点击右上角即可分享
微信分享提示