判断合法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函数对象

    秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象,具有属性(可以使用dir()查询).作为对象,它还可以赋值给其它对象名,或者作为参数传递. lambda函数 在展开 ...

  2. tail-head

    [root@rusky]# tail test3 #不加参数默认显示全部内容 line line2 line3 line4 line5 line6 line7 line8 line9 line10 [ ...

  3. ExifInterface 多媒体文件附加信息

    简介         ExifInterface类主要描述多媒体文件比如JPG格式图片的一些附加信息,包括拍摄时的光圈.快门.白平衡.ISO.焦距.日期时间等各种和拍摄条件以及相机品牌.型号.色彩编码 ...

  4. log4net根据日志类型写入到不同的文件中

    <?xml version="1.0"?> <configuration> <configSections> <!--log4net配置安 ...

  5. Hyper-v虚拟机上网

    Windows 8中内置的Hyper-V管理器可以说给许多人带来了惊喜!在Hyper-V管理器强大的同时,也同样面临着设置中一些不可避免的麻烦.有人说,Hyper-V虚拟机联网麻烦,其实,只要掌握了技 ...

  6. FileDirLocationOperator - 文件或目录位置操作.

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Move ...

  7. KindleRSS推送服务器搭建

    参考http://xcode.so/2010/12/google-gae-rss-to-kindle/这篇文章 1.首先尝试在本机搭建服务器直接推送到kindle 需要使用到kindlereader这 ...

  8. javascript之事件绑定

    曾经写过一篇随笔,attachEvent和addEventListener,跟本文内容有很多相似之处 本文链接:javascript之事件绑定 1.原始写法 <div onclick=" ...

  9. js学习笔记——数组方法

    join() 把数组中所有元素转化为字符串并连接起来,并返回该字符串, var arr=[1,2,3]; var str=arr.join("#"); //str="1# ...

  10. MySQL简单查询

    1.普通查询 select * from info; #查询所有内容 select Code,Name from Info #查询某几列 2.条件查询 select * from Info where ...