qt使用正则表达式限制输入框demo

这个博客介绍了如何使用Qt进行正则表达式的封装,以限制lineEdit的输入内容。作者提供了一个名为CtRegExp的类,包含了多种常见类型的正则表达式,如IPv4地址、端口号、字母数字组合等,并且可以方便地为lineEdit设置验证器。通过引入ctregexp.pri文件,开发者可以直接在项目中使用这些预定义的正则表达式限制输入。
摘要由CSDN通过智能技术生成
简介

对正则表达式进行封装,限制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));
}
结果展示:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅笑一斤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值