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同时生效。
转载于:https://www.cnblogs.com/lvdongjie/p/4353112.html
QT正则表达式---针对IP地址相关推荐
- python正则匹配ip地址_Python正则表达式匹配ip地址实例
Python正则表达式匹配ip地址实例 更新时间:2014年10月09日 11:22:51 投稿:shichen2014 这篇文章主要介绍了Python正则表达式匹配ip地址实例,通过简单的实例讲述了 ...
- python re正则提取ip地址_python 正则表达式匹配IP地址
一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验目的 从tex ...
- shell脚本通过正则表达式提取 IP 地址
假设需要提取的 IP 地址在文件 test.txt 中,可以使用以下 shell 脚本通过正则表达式提取 IP 地址并打印 grep -E -o "([0-9]{1,3}.){3}[0-9] ...
- java正则匹配ip_使用正则表达式匹配IP地址
如何使用正则表达式匹配IP地址?这是很常见的任务.说难也难,说易也易,取决于在匹配的精确度以及正则表达式复杂度之间很好的折中. 通常,IP地址以点分十进制方式表示,IP地址分为4段,以点号分隔.要对I ...
- 内网ip正则表达式php,PHP使用正则表达式判断IP地址是否正确
IP 地址想必大家都很熟悉,由 4 组 0~255 之间的数字组成,每个数字之间使用英文句号"."分割,例如 192.168.0.1 就是一个有效的 IP 地址.想要正确的使用正则 ...
- ip的正则表达式 java,项目实战:Java正则表达式验证IP地址
IP 地址是网络上每台计算机的唯一标识,因此 IP 地址的错误输入将使程序无法运行.下面使用前面学习的正则表达式知识,编写一个 Java 程序来验证 IP 地址是否合法. 在定义正则表达式之前,首先要 ...
- 1.23 实例:正则表达式验证IP地址
IP 地址是网络上每台计算机的唯一标识,因此 IP 地址的错误输入将使程序无法运行.下面使用前面学习的正则表达式知识,编写一个 Java 程序来验证 IP 地址是否合法. 在定义正则表达式之前,首先要 ...
- python正则匹配ip地址_python 正则表达式匹配IP地址
一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验目的 从tex ...
- java正则匹配ip_正则表达式匹配ip地址
iPv4的ip地址都是(1~255).(0~255).(0~255).(0~255)的格式 下面给出相对应的正则表达式: "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9] ...
- java正则匹配ip_正则表达式 - 匹配 IP 地址
17220161221160719 IP地址是指互联网协议地址(英语:Internet Protocol Address,又译为网际协议地址),是IP Address的缩写.IP地址是IP协议提供的一 ...
最新文章
- nginx+uwsgi+django1.6 配置过程
- FMDB:中的用法介绍
- 求1!+2!+3!+...+10!的值
- QT的QThread类的使用
- 【两种解法】he Falling Leaves UVA - 699
- (07)Vivado约束顺序
- nodejs原始连接mysql
- 漫谈广告竞价模式(四)
- 通过DBLINK跨数据库查询,同步创建表结构,插入表数据
- Atitit webdav的使用与配置总结attilax总结 目录 1. 支持的协议	2 1.1. http File unc	2 2. 应用场景	2 2.1. 远程文件管理实现功能 文件建立
- c语言山东科技大学答案oj,山东科技大学oj部分题目记答案.doc
- 2011—2018年软考中级数据库系统工程师历年真题
- .net DLL反编译文件
- CAD学习笔记中级课【坐标】
- day15爬虫(二手房数据)
- 浩顺 HS-320 打印机驱动
- OneHotEncoder简单用法
- 妈妈见我来了的香港旅游局
- USB转I2C芯片操作EEPROM--CH347应用
- Linux查看服务器配置
热门文章
- ubuntu下使用screen用不关闭
- 严重性	代码	说明	项目	文件	行 警告	C4819	该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失	opencv-05	d:\opencv\o
- oracle批量文件入库,C++ Oracle批量高效入库
- 2021-07-31mysql 登录退出
- IDEA java 运行程序 指定运行参数
- Hyperledger Fabric 节点类型Commiter、Endorser、Leader、Anchor
- php 如何将xml转为数组array
- 关于账号密码校验的两种IO流操作
- 基于SSM的医学健康网站
- 五连阳回调买入法_一旦出现“4连阳+1阴”形态,坚定满仓买入,百分百是主升浪...