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 #include4 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 #include5 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 #include4 #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 107"geometry"> 6 137 120 80 9629 10488 11"windowTitle"> 14 <string>MainWindowstring> 15 16class="QWidget" name="centralwidget"> 17 106class="QPushButton" name="pushButton"> 18 30"geometry"> 19 2620 25510 21260 2281 2371 24"text"> 27 <string>PushButtonstring> 28 29class="QLabel" name="label"> 31 43"geometry"> 32 3933 3810 340 3554 3612 37"text"> 40 <string>原始数据:string> 41 42class="QLabel" name="label_2"> 44 56"geometry"> 45 5246 5110 47240 4881 4916 50"text"> 53 <string>正则表达式:string> 54 55class="QTextEdit" name="textEdit"> 57 72"geometry"> 58 6559 6410 6020 61481 62211 63"readOnly"> 66 <bool>falsebool> 67 68"placeholderText"> 69 <string/> 70 71class="QTextEdit" name="textEdit_2"> 73 82"geometry"> 74 8175 8010 76260 77481 7881 79class="QLabel" name="label_3"> 83 95"geometry"> 84 9185 9010 86340 8754 8812 89"text"> 92 <string>结果:string> 93 94class="QTextEdit" name="textEdit_3"> 96 105"geometry"> 97 10498 10310 99360 100481 101111 102108 109