Qt5 unicode解码以及正则匹配
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QDebug>
#include<QString>
#include<QRegularExpression>
#include<QRegularExpressionMatch>
#include<QRegularExpressionMatchIterator>
#include<QStringList>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
QString unicodeToQString(QString rawstr);
void RegularExpression(QString HTML,QString re);//正则提取所需要的字段
QString titleResult="";
QString timeResult="";
QString typeResult="";
QString levelselfResult="";
QString levelfinalResult="";
~MainWindow();
private slots:
void on_pushButton_get_clicked();
void on_comboBox_currentTextChanged(const QString &arg1);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList choose;
choose<<"创建时间"<<"漏洞名称"<<"漏洞类型"<<"自我定级"<<"审核结果";
ui->comboBox->addItems(choose);
this->setWindowFlags(Qt::WindowCloseButtonHint|Qt::WindowMinimizeButtonHint);
//使用resource里的图片作为icon
this->setWindowIcon(QIcon(":/new/prefix1/icon"));
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::unicodeToQString(QString rawstr)
{
do {
int idx = rawstr.indexOf("\\u");
QString strHex = rawstr.mid(idx, 6);
strHex = strHex.replace("\\u", QString());
int nHex = strHex.toInt(0, 16);
rawstr.replace(idx, 6, QChar(nHex));
} while (rawstr.indexOf("\\u") != -1);
return rawstr;
}
void MainWindow::on_pushButton_get_clicked()
{
this->titleResult=this->timeResult=this->typeResult=this->levelselfResult=this->levelfinalResult="";
ui->comboBox->setCurrentIndex(0);
QString rawstr=ui->textEdit_rawstr->toPlainText();
QString str=this->unicodeToQString(rawstr);
QString re="\\\"title\":\\\"(?<title>.*?)\\\".*?\\\"created_at\\\":\\\"(?<time>.*?)\\\".*?\\\"cate_name\\\":\\\"(?<type>.*?)\\\".*?\\\"level_self_label\\\":\\\"(?<level_self_label>.*?)\\\".*?\\\"level_final_label\\\":\\\"(?<level_final_label>.*?)\\\"";
//调用正则提取
RegularExpression(str,re);
ui->textEdit_result->setText(this->timeResult);
//qDebug()<<str;
}
//正则提取所需要的字段
void MainWindow::RegularExpression(QString HTML,QString re)
{
QString TextAfterRe="";
QRegularExpression Re(re);
QRegularExpressionMatchIterator Matchs=Re.globalMatch(HTML);
QRegularExpressionMatch match=Matchs.next();
this->titleResult=this->titleResult+match.captured("title")+'\n';
this->timeResult=this->timeResult+match.captured("time")+'\n';
this->typeResult=this->typeResult+match.captured("type")+'\n';
this->levelselfResult=this->levelselfResult+match.captured("level_self_label")+'\n';
this->levelfinalResult=this->levelfinalResult+match.captured("level_final_label")+'\n';
while(Matchs.hasNext()==true)
{
match=Matchs.next();
this->titleResult=this->titleResult+match.captured("title")+'\n';
this->timeResult=this->timeResult+match.captured("time")+'\n';
this->typeResult=this->typeResult+match.captured("type")+'\n';
this->levelselfResult=this->levelselfResult+match.captured("level_self_label")+'\n';
this->levelfinalResult=this->levelfinalResult+match.captured("level_final_label")+'\n';
}
}
void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
if(ui->comboBox->currentText()=="创建时间")
ui->textEdit_result->setText(this->timeResult);
if(ui->comboBox->currentText()=="漏洞名称")
ui->textEdit_result->setText(this->titleResult);
if(ui->comboBox->currentText()=="漏洞类型")
ui->textEdit_result->setText(this->typeResult);
if(ui->comboBox->currentText()=="自我定级")
ui->textEdit_result->setText(this->levelselfResult);
if(ui->comboBox->currentText()=="审核结果")
ui->textEdit_result->setText(this->levelfinalResult);
}