简介
对正则表达式进行封装,限制lineEdit的输入,该demo编译运行正常,操作正常,可以直接在pro中引入pri文件,该demo下载路径:https://download.csdn.net/download/linyibin_123/86363634
核心文件
ctregexp.pri
HEADERS += \
$$PWD/ctregexp.h
SOURCES += \
$$PWD/ctregexp.cpp
ctregexp.h
#ifndef CTREGEXP_H
#define CTREGEXP_H
#include <QObject>
#include <QRegExpValidator>
class CtRegExp : public QObject
{
Q_OBJECT
public:
enum RegExpType
{
Re_IPv4,
Re_Port,
Re_LettersAndNumbers,
Re_PhoneNumber,
Re_Number,
Re_TwoBitHex,
Re_0To10,
Re_0To32,
Re_0To100,
Re_0To255,
Re_0To300,
Re_0To1000,
Re_0To65535,
Re_1To255,
Re_1To1000,
Re_1To65535,
Re_0P000To10,
Re_0P000To100,
Re_0P000To65535,
Re_10P000To65535,
Re_user,
Re_easypwd,
Re_pwd,
Re_title
};
static QRegExp getQRegExp(RegExpType type);
static QRegExpValidator* getValidator(RegExpType type);
signals:
public slots:
private:
explicit CtRegExp(QObject *parent = nullptr);
};
#endif // CTREGEXP_H
ctregexp.cpp
#include "CtRegExp.h"
QRegExp CtRegExp::getQRegExp(CtRegExp::RegExpType type)
{
switch (int(type)) {
case Re_IPv4:
return QRegExp("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}");
case Re_Port:
return QRegExp("(([1-9]\\d{0,3})|([1-5]\\d{4})|(6([0-4]\\d{0,3}|5([0-4]\\d{0,2}|5([0-2]\\d|3[0-5])))))");
case Re_LettersAndNumbers:
return QRegExp("[0-9a-zA-Z]+");
case Re_Number:
return QRegExp("\\d+");
case Re_PhoneNumber:
return QRegExp("(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}");
case Re_TwoBitHex:
return QRegExp("^([0-9a-fA-F]{4})$");
case Re_0To10:
return QRegExp("(\\d|10)");
case Re_0To32:
return QRegExp("(\\d|[0-2]\\d|3[0-2])");
case Re_0To100:
return QRegExp("\\d{1,2}|100");
case Re_0To255:
case Re_0To1000:
return QRegExp("\\d{1,3}|1000");
case Re_0To65535:
return QRegExp("([1-5]?\\d{1,4})|(6([0-4]\\d{0,3}|5([0-4]\\d{0,2}|5([0-2]\\d|3[0-5]))))");
case Re_1To255:
return QRegExp("(1?[1-9]\\d{0,1}|2([0-4]\\d|5[0-5]))");
case Re_1To1000:
return QRegExp("([1-9]|[1-9]\\d{1,2}|1000)");
case Re_1To65535:
return QRegExp("(([1-9]\\d{0,3})|([1-5]\\d{4})|(6([0-4]\\d{0,3}|5([0-4]\\d{0,2}|5([0-2]\\d|3[0-5])))))");
case Re_0P000To10:
return QRegExp("([0-9]|[0-9]\\.[0-9]{1,3}|10)");
case Re_0P000To100:
return QRegExp("([0-9]|[0-9]\\.[0-9]{1,3}|[1-9][0-9]|[1-9][0-9]\\.[0-9]{1,3}|100)");
case Re_0P000To65535:
return QRegExp("([0-9]|[0-9]\\.[0-9]{1,3}|[1-9][0-9]|[1-9][0-9]\\.[0-9]{1,3}|[1-9]\\d{2}|[1-9]\\d{2}\\.[0-9]{1,3}|[1-9]\\d{3}|[1-9]\\d{3}\\.[0-9]{1,3}|[1-5]\\d{4}|[1-5]\\d{4}\\.[0-9]{1,3}|[6][0-4]\\d{3}|[6][0-4]\\d{3}\\.[0-9]{1,3}|[6][5][0-4]\\d{2}|[6][5][0-4]\\d{2}\\.[0-9]{1,3}|[6][5][5][0-2][0-9]|[6][5][5][0-2][0-9]\\.[0-9]{1,3}|[6][5][5][3][0-4]|[6][5][5][3][0-4]\\.[0-9]{1,3}|65535)");
case Re_10P000To65535:
return QRegExp("([1-9][0-9]|[1-9][0-9]\\.[0-9]{1,3}|[1-9]\\d{2}|[1-9]\\d{2}\\.[0-9]{1,3}|[1-9]\\d{3}|[1-9]\\d{3}\\.[0-9]{1,3}|[1-5]\\d{4}|[1-5]\\d{4}\\.[0-9]{1,3}|[6][0-4]\\d{3}|[6][0-4]\\d{3}\\.[0-9]{1,3}|[6][5][0-4]\\d{2}|[6][5][0-4]\\d{2}\\.[0-9]{1,3}|[6][5][5][0-2][0-9]|[6][5][5][0-2][0-9]\\.[0-9]{1,3}|[6][5][5][3][0-4]|[6][5][5][3][0-4]\\.[0-9]{1,3}|65535)");
case Re_user:
return QRegExp("(^[a-zA-Z0-9-\u4e00-\u9fa5_ ]+$)");
case Re_easypwd:
return QRegExp("([0-9A-Za-z]{8,16})");
case Re_pwd:
return QRegExp("([^@#%:?/]*)");
case Re_title:
return QRegExp("(^[a-zA-Z0-9-\u4e00-\u9fa5_ ]+$)");
}
return QRegExp();
}
QRegExpValidator *CtRegExp::getValidator(RegExpType type)
{
return new QRegExpValidator(getQRegExp(type));
}
CtRegExp::CtRegExp(QObject *parent) : QObject(parent)
{
}
使用:
#include "myregexp.h"
#include "ui_myregexp.h"
#include "ctregexp.h"
MyRegExp::MyRegExp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyRegExp)
{
ui->setupUi(this);
initConfig();
}
MyRegExp::~MyRegExp()
{
delete ui;
}
void MyRegExp::initConfig()
{
ui->lineEdit->setValidator(CtRegExp::getValidator(CtRegExp::Re_IPv4));
ui->lineEdit_2->setValidator(CtRegExp::getValidator(CtRegExp::Re_Port));
ui->lineEdit_3->setValidator(CtRegExp::getValidator(CtRegExp::Re_user));
ui->lineEdit_4->setValidator(CtRegExp::getValidator(CtRegExp::Re_easypwd));
ui->lineEdit_5->setValidator(CtRegExp::getValidator(CtRegExp::Re_title));
}