QT Regular Expressions---for IP addresses

Source: Internet
Author: User

To determine the QT regular expression of a legitimate IP:

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 ("error"), TR ("IP address Error"));

return false;

}
}

if (Rx2.cap (7). ToInt () ==0)
{

Qmessagebox::information (This, tr ("error"), TR ("IP address Error"));

return false;

}

if (Rx2.cap (7). ToInt () ==0)
{
Qmessagebox::information (This, tr ("error"), TR ("IP address Error"));

return false;

}
}
Else
{

Qmessagebox::information (This, tr ("error"), TR ("IP address Error"));

return false;

}

return true;

}

An easier way to determine an IP address is to:

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 ("error"), TR ("IP address Error"));

return false;

}

return true;

Determine if the file name contains characters other than letters, numbers, and underscores:

BOOL Isrightfilename (QString filename)

{

Qregexp Rx ("[a-za-z_0-9]+");

if (!rx.exactmatch (filename))

{

Qmessagebox::information (This, tr ("error"), tr ("file name contains other characters or Chinese characters");

return false;

}

return true;

}

use regular expressions to verify the legality of the IP. Transferred from: http://yleesun.blog.163.com/blog/static/29413402200952464324323/

Regular Expressions:

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;"); the implementation of the framework of verify the legitimacy of the input IP, subnet mask. (Generate IP Address Class) from: 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", +, 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 ());
} Sets the input box mode with an IP input format. Turn from: Http://www.qtcn.org/bbs/simple/?t28473.htmlQRegExp 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 (&AMP;V);
Le.setinputmask ("000.000.000.000;0");//Just add it; 0 guaranteed to have a default value allows the regular and mask to take effect simultaneously.

QT Regular Expressions---for IP addresses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.