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/p/3802561.html,如需转载请自行联系原作者

相关文章
|
Ubuntu
Ubuntu :relocation R_X86_64_32 against `.rodata‘ can not be used when making a PIE object;
Ubuntu :relocation R_X86_64_32 against `.rodata‘ can not be used when making a PIE object;
2001 0
Ubuntu :relocation R_X86_64_32 against `.rodata‘ can not be used when making a PIE object;
|
计算机视觉
Qt实用技巧:代码中QIcon缩放(QPixmap的手动放大和QIcon自动缩小)
Qt实用技巧:代码中QIcon缩放(QPixmap的手动放大和QIcon自动缩小)
Qt实用技巧:代码中QIcon缩放(QPixmap的手动放大和QIcon自动缩小)
|
UED Python Windows
Python Qt GUI设计:QTimer计时器类、QThread多线程类和事件处理类(基础篇—8)
Python Qt GUI设计:QTimer计时器类、QThread多线程类和事件处理类(基础篇—8)
Python Qt GUI设计:QTimer计时器类、QThread多线程类和事件处理类(基础篇—8)
QT应用编程: QTableWidget删除选中的多行与全选、取消全选
QT应用编程: QTableWidget删除选中的多行与全选、取消全选
812 0
QT应用编程: QTableWidget删除选中的多行与全选、取消全选
|
物联网 Linux Android开发
|
C语言 编译器 NoSQL
查看和调试Qt源码
简述 在调试程序的时候,有时需要调试进入 Qt 源码,这不仅有利于我们了解内部实现机制,而且对于解决一些隐蔽性问题很有帮助。 都知道 F11 是“单步进入”,可是在调试的过程中,按下 F11 却无法进入。有人说需要静态编译 Qt 才可以,其实不然,只需要简单的几个配置就可以搞定。 简述 Qt 版本及安装 配置 Qt Creator 源码调试 更多参考
3015 0
|
C++ Android开发
3.QT中QCommandLineParser和QCommandLineOption解析命令行参数
 1  新建项目 main.cpp #include &lt;QCoreApplication&gt; #include &lt;QCommandLineParser&gt; #include &lt;QDebug&gt; #include &lt;stdio.h&gt;   in
2202 0
相关产品
云迁移中心
推荐文章
更多
推荐镜像
更多