判断合法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的合法性。转自:http://yleesun.blog.163.com/blog/static/29413402200952464324323/

正则表达式:

   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地址类)转自:http://www.qtcn.org/bbs/simple/?t18020.html
//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输入格式的输入框模式。转自:http://www.qtcn.org/bbs/simple/?t28473.html
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同时生效。

QT正则表达式---针对IP地址的更多相关文章

  1. 正则表达式检测IP地址与端口号是否合法

    正则表达式检测IP地址与端口号是否合法,代码如下: 正则表达式检测IP地址 public static bool CheckAddress(string s) { bool isLegal = fal ...

  2. 使用正则表达式匹配IP地址

    IP地址分为4段,以点号分隔.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配:   第一步:地址分析,正则初判 1.0-9 \d 进行匹配 2.10-99 [1-9]\d 进行匹 ...

  3. python中利用正则表达式匹配ip地址

    现在有一道题目,要求利用python中re模块来匹配ip地址,我们应如何着手? 首先能想到的是ip地址是数字,正则表达式是如何匹配数字的呢? \d或[0-9] 对于这个问题,不要一下子上来就写匹配模式 ...

  4. grep使用正则表达式搜索IP地址

    递归搜索当前目录及其子目录.子目录的子目录……所包含文件是否包含IP地址 grep -r "[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit: ...

  5. qt获取网络ip地址的类

    最近在学习qt网络编程,基于tcp和udp协议. 看了一些别人的程序和qt4自带的例子,困扰我最大的问题就是获取ip的类,总结起来还挺多的. 主要介绍常用的QtNetwork Module中的QHos ...

  6. python 正则表达式匹配IP地址

    一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验目的 从tex ...

  7. 正则表达式验证IP地址(绝对正确)

    正则验证合法_有效的IP地址(ipv4/ipv6) 不墨迹直接上代码: 正则表达式: /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[ ...

  8. python 利用正则表达式获取IP地址

    例:import retest= '$MYNETACT: 0,1,"10.10.0.9"'pattern =re.compile(r'"(\d+\.\d+\.\d+\.\ ...

  9. 正则表达式判断ip地址

    html: <div class="configuration"><form action="" name="myformcon&q ...

随机推荐

  1. Python序列的方法(转)

    在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值表(tuple)和表(list).此外,字符串(string)是一种特殊的定值表.表的元素可以更改,定值表一旦建立,其 ...

  2. 配置squid代理服务

    1. 简述一下squid的用途?squid可以做代理和缓存服务器,而做代理时,可以分为正向代理和反向代理.正向代理用在企业办公环境中,企业员工上网通过代理来上网,代理的缓存功能可以为企业节省宝贵的带宽 ...

  3. 关于winform主题IrisSkin2的编写

    第一步:首先引用IrisSkin2.dll. 第二步自定义类: /// <summary> /// 窗体主题边界类 /// </summary> public class Fo ...

  4. DNN7网站系统需求及部署指南详解

    此安装指南适用于DNN6.x和DNN7.x在本地测试及主机的安装.最近QQ群里不少朋友问我关于DotNetNuke的安装和运行的问题. 为了让大家更清楚地了解DNN的安装方式,我在这里对DotNetN ...

  5. NodeJs获取不到POST参数

    NodeJs报错,从网页端获取不到POST参数,提示错误类似如下 TypeError: Cannot read property 'username' of undefined     at C:\U ...

  6. Visual Studio 2015开发Android App问题集锦

    Visual Studio 2015开发Android App 启动调试始终无法完成应用部署的解决方案 创建一个Android App项目后,直接启动调试发现Visual Studio Emulato ...

  7. iOS_SN_百度地图基本使用(1)

    上次用了一次百度地图,一直没有记笔记,今天记一笔. 以前没有用过百度地图的时候,听做这方面的朋友说百度地图有不少的坑,但是我做的时候没有遇到太大的坑,主要是要注意官方文档的注意事项,还有配置环境开发中 ...

  8. Ueditor使用方法

    1.到百度下载文件,有各种版本.下载.net版本 2.将所需文件导入工程中 分别是:themes文件夹.third-party文件夹.ueditor.all.min.js.ueditor.config ...

  9. 禁止chrome中CORS跨域资源共享错误

    在开发中,可以通过命令行命令chrome --allow-file-access-from-files来 禁止CORS错误. 只在紧急情况下使用这个方法,比如你的老板正站在你身后, 并且所有事情都无法 ...

  10. jQuery学习资源参考教程网址推荐

    jQuery官方主页:http://jquery.comjQuery中文入门指南:http://www.k99k.com/jQuery_getting_started.htmljQuery使用手册:h ...