博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT正则表达式---针对IP地址
阅读量:4326 次
发布时间:2019-06-06

本文共 3111 字,大约阅读时间需要 10 分钟。

 

判断合法IP的QT正则表达式:

bool IsIPaddress(QString ip)

{

    QRegExp rx2("(//d+)(//.)(//d+)(//.)(//d+)(//.)(//d +)");

    int pos = rx2.indexIn(ip);

    if(pos>-1)
    {

         for(int i=0;i<4;i++)

        {
            if( rx2.cap(i*2+1).toInt()>=255 )
            {
                QMessageBox::information(this, tr("错误"), tr("IP地址错误"));

                return false;

            }

        }

        if(rx2.cap(7).toInt()==0)

        {           

            QMessageBox::information(this, tr("错误"), tr("IP地址错误"));

            return false;

        }

        if(rx2.cap(7).toInt()==0)
        {
            QMessageBox::information(this, tr("错误"), tr("IP地址错误"));

            return false;

        }

    }
    else
    {

        QMessageBox::information(this, tr("错误"), tr("IP地址错误"));

        return false;

    }

    return true;

}

判断IP地址更简单的方式是:

    QRegExp rx2("^([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])$")

    if( !rx2.exactMatch(ip) )

    {

          QMessageBox::information(this, tr("错误"), tr("ip地址错误"));

          return false;

    }

    return true;

判断文件名是否含有字母、数字、下划线之外的字符:

bool IsRightFilename(QString filename)

{

    QRegExp rx("[A-Za-z_0-9]+");

    if( !rx.exactMatch( filename) )

    {

          QMessageBox::information(this, tr("错误"), tr("文件名含有其他字符或中文字符"));

          return false;

    }

    return true;

}

使用正则表达式检验IP的合法性。转自:

 正则表达式:

   QRegExp rx ((2[0-4]//d|25[0-5]|[01]?//d//d?)//.){3}(2[0-4]//d|25[0-4]|[01]?//d//d?) ;
    ipLabel = new QLabel(tr("IP Address:"));
    ipLineEdit = new QLineEdit;
    ipLabel->setBuddy(ipLineEdit);
    QValidator *validator = new QRegExpValidator(rx, this);
    ipLineEdit->setValidator(validator);
    ipLineEdit->setInputMask("000.000.000.000;");
 
实现框架下的
验证输入的IP、子网掩码的合法性。(生成IP地址类)转自:
//ip.h
#include <qwidget.h>
#include <qstring.h>
#include <qlineedit.h>
#include <qvalidator.h>
#define IP_H
class IP : public QWidget
{
    public:
        IP ( const QString & text, QWidget *parent, const char *name );
        QString getValue();
    private:
        QLineEdit * ip[4];
        QIntValidator * ipv[4];
};
//ip.cpp
#include <qwidget.h>
#include <qlabel.h>
#include <qfont.h>
#include <qhbox.h>
#include <qlineedit.h>
#include "ip.h"
IP::IP(const QString & text, QWidget *parent, const char *nombre) : QWidget(parent, nombre, 0) 
{
    QFont *fuente = new QFont("Arial", 14, QFont::Normal);
    fuente->setPixelSize(14);
    QLabel *label = new QLabel( this );
    label->setFont(* fuente);
    label->setMinimumWidth(140);
    label->setMaximumWidth(140);
    QHBox * ipp = new QHBox((QWidget*) this);
    ipp->setMinimumWidth(140);
    ipp->setMaximumWidth(140);
    for (int i=0; i<4; i++)
    {
        ip = new QLineEdit((QWidget*) ipp, nombre);
        ip->setFont(* fuente);
        ip->setMinimumWidth(30);
        ip->setMaxLength(3);
        ipv = new QIntValidator(0, 255, (QObject*)ipp);
        ip->setValidator(ipv);
    }
    label->move(0, 0);
    ipp->move(150, 0);
    label->setText(text);
    // ip->setInputMask("000.000.000.000; ");
}
QString IP::getValue()
{
    bool flag = false;
    for (int i=0; i<4; i++)
        if ( ip->text().isEmpty() )
            flag = true;
    if (flag)
        return QString("0.0.0.0");
    else
        return QString(ip[0]->text()+ "." + ip[1]->text() + "." + ip[2]->text() + "." +
                ip[3]->text());
}
设置具有IP输入格式的输入框模式。转自:
QRegExp rx("((2[0-4]//d|25[0-5]|[01]?//d//d?)//.){3}(2[0-4]//d|25[0-5]|[01]?//d//d?)");
QRegExpValidator v(rx, 0);
QLineEdit le;
le.setValidator(&v);
le.setInputMask("000.000.000.000;0");//只要加上;0保证有默认值即可使得正则和mask同时生效。

转载于:https://www.cnblogs.com/lvdongjie/p/4353112.html

你可能感兴趣的文章
JS匿名执行函数
查看>>
关于InputStream类的available()方法
查看>>
六边形架构模式
查看>>
HAOI2007 理想的正方形 单调队列
查看>>
(原创)c#学习笔记04--流程控制04--循环03--for循环
查看>>
从控制台输入一个五位数,计算各个数位之和
查看>>
为Sublime Text 3设置优雅的字体
查看>>
Eclipse使用Jetty(转)
查看>>
vim命令收集(持续中)
查看>>
Zynq -- 启动过程
查看>>
206. Reverse Linked List(LeetCode)
查看>>
day 04 Java并发多线程
查看>>
Java定时任务调度工具Timer Quartz
查看>>
WPF,Silverlight与XAML读书笔记第三十五 - 可视化效果之图像
查看>>
Nginx中location配置[转]
查看>>
编程重点
查看>>
00-A-springmvc分布式项目项目结构
查看>>
vs调试时报503错误
查看>>
SVN使用&CVS使用
查看>>
redis
查看>>