正则->匹配ip

导言:   ip是0.0.0.0到255.255.255.255,其中数字不能是012之类0开头,必须是12这样子的;

ip匹配的正则完整表达式如下:

(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)) {3};

ip中的数字0-255的正则是25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d;

25[0-5] 的数字范围是250-255;

2[0-4]\\d的数字范围是 200-249,其中\\d是0-9的数字;

1\\d{2}的数字范围是100-199,\\d后面的{2}是代码出现2次;

[1-9]?\\d的数字范围是0-99,[1-9]后面的?是代码出现0次或者1次;

表达式中的\\.代表. 所以(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)) {3}代表.(0-255)出现三次

综上所述, (25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)) {3};可以匹配

0.0.0.0到255.255.255.255的ip地址

测试章节 https://blog.csdn.net/u013291321/article/details/80497029


详细说明这段代码的优化方法Ping::Ping(QObject* parent) : QObject(parent), failCount(0) { process = new QProcess(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout())); connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadyReadStandardOutput())); connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onReadyReadStandardError())); } void Ping::startPing(QString ipAddress) { // Stop the ping command if it's running stopPing(); // Clear fail counter failCount = 0; // Start the ping command with appropriate arguments this->ipAddress = ipAddress; QStringList arguments; qDebug()<<"ip"<<ipAddress<<this->ipAddress; arguments << "-n" << "1" << "-w" << "1000" << ipAddress; process->start("ping", arguments); // arguments<< "-a" << ipAddress; // process->start("arp", arguments); // Start the timer to repeatedly send the ping command timer->start(1000); // ping every 1 second } void Ping::stopPing() { // Stop the ping command process->kill(); process->waitForFinished(); // Stop the timer timer->stop(); } void Ping::onTimeout() { failCount++; if (failCount >= 3) { QString macAddress = ""; emit deviceDisconnected(ipAddress, macAddress); stopPing(); } else { onPing(); } } void Ping::onPing() { // Write a newline to the ping process to send another ping //process->write("\n"); QStringList arguments; arguments << "-n" << "1" << "-w" << "1000" << ipAddress; process->start("ping", arguments); } void Ping::onReadyReadStandardOutput() { process->waitForFinished(); QByteArray output(process->readAllStandardOutput()); QString str = QString::fromLocal8Bit(output); if (str.contains("丢失 = 0")) { emit deviceConnected(ipAddress, ""); failCount = 0; } } void Ping::onReadyReadStandardError() { // Output the standard error of the ping command to the console QString output(process->readAllStandardError()); qDebug()<<"errormessage" << output; }
最新发布
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值