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 ...
随机推荐
- 输入一个字符串,输出时数字倒序。例如:输入"hello2345wo7654",输出则为"hello5432wo4567"
public class ReserveString { public static void main(String[] args) { System.out.println("Pleas ...
- mysql ERROR 1064 (42000): Erreur de syntaxe près de 'order)
mysql> INSERT INTO page (author_username, page_title, addtime, cat_id, page_content,author_uid,it ...
- Hibernate简单的基础理论
和Hibernate有关的概念,是掌握Hibernate必须了解的知识.就个人经验来说,可以在了解如何简单开发Hibernate之后,再来学习这些概念,这样可以有个比较清楚的认识.Hibernate是 ...
- shell中的eval
eval语法 eval arg1 arg2 ... eval的作用就是将后面的参数arg1 arg2等等当成一个pipeline,然后重新执行shell处理pipeline的流程(有关pipeline ...
- javascript学习教程之---如何从一个tab切换到banner幻灯片的转换
一个简单的tab切换代码: <!doctype html> <html> <head> <meta charset="utf-8"> ...
- php 之 分页查询的使用方法及其类的封装
一.分页的使用: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- Java学习笔记--Collection和Collections的区别
转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别. 1.java.util.Collection 是一个集合 ...
- iOS开发之多媒体API(1)
iOS开发之多媒体API(1) 播放视频 视频文件介绍 视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类.尽管后者在播放的稳定性和播放画面质量上可能没 ...
- 配置TC2.0运行环境
一. 学习过程 下载TC2.0并打开如图: 这是一个集成的C语言环境包,包括TC.dosbox和一些编译工具,很明显这样我无法判断单个程序的功能,也无法区分哪些程序是必须的. 那么为了搞清楚哪些文件是 ...
- 使用 ExpandableListView 实现折叠ListView
1:layout/expandablelistview_groups.xml 标题文件 <?xml version="1.0" encoding="utf-8&qu ...