判断合法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. Java基础知识强化68:基本类型包装类之Character概述和Character常见方法

    1. Character概述: public final class Character extends Object implements Serializable,Comparable<Ch ...

  2. python课程第一天作业1-模拟登录

    第一周作业: 作业1:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 流程图: 代码:后来修改过一次: #!/usr/bin/env python # -*-conding:ut ...

  3. canvas-画七巧板

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  4. DataGrid( 数据表格) 组件[6]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  5. DOM中的NodeList与HTMLCollection

    最近在看<Javascript高级程序设计>的时候,看到了这样一句话:“理解NodeList和HTMLCollection,是从整体上透彻理解DOM的关键所在.”,所以觉得应该写一篇关于N ...

  6. VS2012发布网站IIS配置

    首先要配置好下面步骤 然后 把图上勾选的都勾选 最后一步 那IIS就配置好了,怎么添加发布呢打开IIS管理器 然后带点击网站添加网站 ,在这之前首先要在磁盘里新建一个文件夹,把项目复制过去,网站随便命 ...

  7. Android -------- 网络访问数据

  8. Android-应用的本地化及知识拓展之配置修饰符

    步骤很简单,只需要两步: 1.创建带有目标语言的配置修饰符的资源子目录 2.将可选资源放入该目录下,android系统会自动处理后续工作 在这里我们需要讲解一下配置修饰符. 中文的配置修饰符:-zh, ...

  9. Google map实现类似Google earth的图标展开功能

    前言 在Google map的开发时,如果有多个图标重叠在一起,这时下面的图标就点击不到.而在Google Earth中,鼠标一移到上面就会自动弹开,这对于用户而言是十分人性化的一个功能.如下是在Go ...

  10. Android再学习-20141111-Android应用的七大件

    Android应用的七大件 应用程序的四大组件: Android的四大组件,使用时需要在程序中注册. Activity: Activity是应用程序的一个界面,可以通过这个界面查看联系人.打电话或者玩 ...