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

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include 
 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 }

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include 
 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

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include 
 4 #include 
 5 #include 
 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)
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 }

mainwindow.ui

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

相关