QT正则表达式---针对IP地址
判断合法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/
正则表达式:
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;");
#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());
}
QRegExpValidator v(rx, 0);
QLineEdit le;
le.setValidator(&v);
le.setInputMask("000.000.000.000;0");//只要加上;0保证有默认值即可使得正则和mask同时生效。
QT正则表达式---针对IP地址的更多相关文章
- 正则表达式检测IP地址与端口号是否合法
正则表达式检测IP地址与端口号是否合法,代码如下: 正则表达式检测IP地址 public static bool CheckAddress(string s) { bool isLegal = fal ...
- 使用正则表达式匹配IP地址
IP地址分为4段,以点号分隔.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配: 第一步:地址分析,正则初判 1.0-9 \d 进行匹配 2.10-99 [1-9]\d 进行匹 ...
- python中利用正则表达式匹配ip地址
现在有一道题目,要求利用python中re模块来匹配ip地址,我们应如何着手? 首先能想到的是ip地址是数字,正则表达式是如何匹配数字的呢? \d或[0-9] 对于这个问题,不要一下子上来就写匹配模式 ...
- grep使用正则表达式搜索IP地址
递归搜索当前目录及其子目录.子目录的子目录……所包含文件是否包含IP地址 grep -r "[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit: ...
- qt获取网络ip地址的类
最近在学习qt网络编程,基于tcp和udp协议. 看了一些别人的程序和qt4自带的例子,困扰我最大的问题就是获取ip的类,总结起来还挺多的. 主要介绍常用的QtNetwork Module中的QHos ...
- python 正则表达式匹配IP地址
一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验目的 从tex ...
- 正则表达式验证IP地址(绝对正确)
正则验证合法_有效的IP地址(ipv4/ipv6) 不墨迹直接上代码: 正则表达式: /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[ ...
- python 利用正则表达式获取IP地址
例:import retest= '$MYNETACT: 0,1,"10.10.0.9"'pattern =re.compile(r'"(\d+\.\d+\.\d+\.\ ...
- 正则表达式判断ip地址
html: <div class="configuration"><form action="" name="myformcon&q ...
随机推荐
- linux修改主机名-IP
1.查看当前主机名 hostname 2. ifconfig 显示所有网络接口的信息 ifconfig eth0 显示网卡eth0的信息 3.临时修改主机名 hostname rusky. ...
- [serverlet][转载: 深入理解HTTP Session]
[serverlet][转载: 深入理解HTTP Session] 标签(空格分隔): 未分类 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. ...
- mongo 初始配置
连接mongo 时 在window的可视化工具 有时会出现这种无法找到表的情况 那么我们所需要的是什么?? 用客户端的命令行 查看是否能够真正连接成功 下载mongo window 并安装 这个网 ...
- 1.引入必要的文件 2.加载 UI 组件的方式 4.Parser 解析器
//引入 jQuery 核心库,这里采用的是 2.0 <scripttype="text/javascript"src="easyui/jquery.min.js& ...
- 使用Uploadify 时,同时使用了jQuery.Validition 验证控件时,在IE11上出现JS缺少对象错误。
场景: 使用jQuery.1.8.2 使用 Uploadify 3.2上传控件 使用jQuery.Validition 1.9 验证 使用IE 11 时,当鼠标点击上传按钮时,会出现JS 缺少对象错误 ...
- javascript "非法值"检验.
<script type="text/javascript"> function getCoord() { var x = document.getElementByI ...
- Swift中的协议
协议: 1.Swift协议用于定义多个类型应该遵守的规范 2.协议定义了一种规范, 不提供任何实现 3.协议统一了属性名, 方法, 下标, 但是协议并不提供任何实现 4.语法格式: [修饰符] pro ...
- 创建自托管的SignalR服务端
微软官方例子地址:http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host 1.说明: SignalR服务端可 ...
- 关于javascript中setTimeout()和clearTimeout()的疑惑。
由于在w3school中学习javascript时,当学到setTimeout()和clearTimeout()方法时.根据它所提供的例子(下面的代码转自w3cschool)—计数程序,发现当你不停的 ...
- Apache配置rewrite
最近将代码做了迁移,更换了web服务器,从原来的Nginx,换成使用Apache,多少有些区别.这里整理一下在apache下实现rewrite功能. 第一部分:修改apache配置文件支持rewrit ...