Qt-Qt使用QRegExp实现正则表达式处理(Qt5.14.2+win10)

相关资料:

实例代码:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class MainWindow; }
 8 QT_END_NAMESPACE
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     MainWindow(QWidget *parent = nullptr);
16     ~MainWindow();
17 
18 private slots:
19     void on_pushButton_clicked();
20 
21 private:
22     Ui::MainWindow *ui;
23 };
24 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include <QDebug>
 4 #include <QTextDocument>
 5 #include <QTextBlock>
 6 
 7 MainWindow::MainWindow(QWidget *parent)
 8     : QMainWindow(parent)
 9     , ui(new Ui::MainWindow)
10 {
11     ui->setupUi(this);
12 
13     setWindowTitle(QStringLiteral("Qt使用QRegExp实现正则表达式处理"));
14 
15     //
16     ui->textEdit->setText("GUID=100\n"
17                           "AAA=98\n"
18                           "tttt=99\n"
19                           "iiii=88\n"
20                           "sdfsdf=9888\n");
21     ui->textEdit_2->setText(".*=.*");
22 }
23 
24 MainWindow::~MainWindow()
25 {
26     delete ui;
27 }
28 
29 
30 void MainWindow::on_pushButton_clicked()
31 {
32     QRegExp rx(ui->textEdit_2->toPlainText());
33 
34     QTextDocument* doc = ui->textEdit->document () ; //文本对象
35     int cnt=doc->blockCount () ;//回车符是一个 block
36 
37     ui->textEdit_3->clear();
38     for (int i=0; i<cnt; i++)
39     {
40         QTextBlock textLine = doc->findBlockByNumber(i) ; // 文本中的一段
41         QString str = textLine.text();
42         bool match = rx.exactMatch(str);
43         if (match)
44           ui->textEdit_3->append(str);
45     }
46 }
View Code

mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>MainWindow</class>
  4  <widget class="QMainWindow" name="MainWindow">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>629</width>
 10     <height>488</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>MainWindow</string>
 15   </property>
 16   <widget class="QWidget" name="centralwidget">
 17    <widget class="QPushButton" name="pushButton">
 18     <property name="geometry">
 19      <rect>
 20       <x>510</x>
 21       <y>260</y>
 22       <width>81</width>
 23       <height>71</height>
 24      </rect>
 25     </property>
 26     <property name="text">
 27      <string>PushButton</string>
 28     </property>
 29    </widget>
 30    <widget class="QLabel" name="label">
 31     <property name="geometry">
 32      <rect>
 33       <x>10</x>
 34       <y>0</y>
 35       <width>54</width>
 36       <height>12</height>
 37      </rect>
 38     </property>
 39     <property name="text">
 40      <string>原始数据:</string>
 41     </property>
 42    </widget>
 43    <widget class="QLabel" name="label_2">
 44     <property name="geometry">
 45      <rect>
 46       <x>10</x>
 47       <y>240</y>
 48       <width>81</width>
 49       <height>16</height>
 50      </rect>
 51     </property>
 52     <property name="text">
 53      <string>正则表达式:</string>
 54     </property>
 55    </widget>
 56    <widget class="QTextEdit" name="textEdit">
 57     <property name="geometry">
 58      <rect>
 59       <x>10</x>
 60       <y>20</y>
 61       <width>481</width>
 62       <height>211</height>
 63      </rect>
 64     </property>
 65     <property name="readOnly">
 66      <bool>false</bool>
 67     </property>
 68     <property name="placeholderText">
 69      <string/>
 70     </property>
 71    </widget>
 72    <widget class="QTextEdit" name="textEdit_2">
 73     <property name="geometry">
 74      <rect>
 75       <x>10</x>
 76       <y>260</y>
 77       <width>481</width>
 78       <height>81</height>
 79      </rect>
 80     </property>
 81    </widget>
 82    <widget class="QLabel" name="label_3">
 83     <property name="geometry">
 84      <rect>
 85       <x>10</x>
 86       <y>340</y>
 87       <width>54</width>
 88       <height>12</height>
 89      </rect>
 90     </property>
 91     <property name="text">
 92      <string>结果:</string>
 93     </property>
 94    </widget>
 95    <widget class="QTextEdit" name="textEdit_3">
 96     <property name="geometry">
 97      <rect>
 98       <x>10</x>
 99       <y>360</y>
100       <width>481</width>
101       <height>111</height>
102      </rect>
103     </property>
104    </widget>
105   </widget>
106  </widget>
107  <resources/>
108  <connections/>
109 </ui>
View Code

 

posted on 2022-02-25 16:24  疯狂delphi  阅读(90)  评论(0编辑  收藏  举报

导航