QT正则表达式---针对IP地址 .
2013-07-04 16:52
239 查看
首先小例:转自http://blog.csdn.net/vah101/archive/2010/12/08/6063180.aspx
判断合法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同时生效。
判断合法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地址系列
- QT正则表达式---针对IP地址
- QT正则表达式---针对IP地址
- QT正则表达式---针对IP地址
- ip地址和身份证的正则表达式验证
- 编写正则表达式,判断给定的是否是一个合法的IP地址。
- Qt之正则表达式 - QRegExp
- python3爬虫之入门和正则表达式,获取IP地址, 隐马尔可夫模型及其在分词中的简单应用
- IP地址的正则表达式
- 验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式
- Qt 正则表达式简单测试
- 正则表达式表示的IP地址
- 常用正则表达式:手机、电话、邮箱、身份证、IP地址、网址、日期等
- 常用正则表达式大全,手机、电话、邮箱、身份证(最严格的验证)、IP地址、网址、日期等,一般前台js验证,来这里就够了...
- IP地址正则表达式匹配方法
- C中使用正则表达式验证email地址和IP地址
- 一个匹配IP地址的正则表达式
- 匹配IP地址的正则表达式实例
- C#正则表达式分解和转换IP地址实例(C#正则表达式大全 c#正则表达式语法)
- 检测IP地址的正则表达式